Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix regression in ArrayHelper::map #20328

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Yii Framework 2 Change Log
------------------------

- Enh #20309: Add custom attributes support to style tags (nzwz)
- Bug #20327: Fix regression in `\yii\helpers\BaseArrayHelper::map` (chriscpty)


2.0.52 February 13, 2025
Expand Down
3 changes: 0 additions & 3 deletions framework/helpers/BaseArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,6 @@ public static function getColumn($array, $name, $keepKeys = true)
*/
public static function map($array, $from, $to, $group = null)
{
if (is_string($from) && is_string($to) && $group === null && strpos($from, '.') === false && strpos($to, '.') === false) {
return array_column($array, $to, $from);
}
$result = [];
foreach ($array as $element) {
$key = static::getValue($element, $from);
Expand Down
28 changes: 28 additions & 0 deletions tests/framework/helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,12 @@ static function (array $group) {
'33' => '44',
'55' => '66'
], $result);

$array = [new MagicClass()];

$result = ArrayHelper::map($array, 'magic', 'moreMagic');

$this->assertEquals([42 => 'ta-da'], $result);
}

public function testKeyExists()
Expand Down Expand Up @@ -1864,3 +1870,25 @@ public function getMoreMagic()
return 'ta-da';
}
}

class MagicClass
{
public function __get($name) {
if ($name === 'magic') {
return 42;
}
if ($name === 'moreMagic') {
return 'ta-da';
}
return $this->$name;
}

public function __set($name, $value) {
$this->$name = $value;
}

public function __isset($name)
{
return isset($this->$name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation is incorrect - it returns false for magic and moreMagic properties.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out - the tests end up succeeding if isset is implemented correctly.

After testing with where I initially noticed the "issue" first, I figured out it's actually another implementation problem on my end 🤦 nevermind this PR and issue lol

}
}