Skip to content

Commit

Permalink
Update StaticAddToTrait.php
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkSide666 authored Mar 20, 2020
1 parent c40a81a commit 8707241
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/StaticAddToTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ private static function _addToWithClassName(object $parent, $seed, bool $unsafe,
$seed = [$seed];
}

$cl = reset($seed);
if (key($seed) !== 0) {
if (!isset($seed[0])) {
throw new Exception('Class name in seed is not defined');
}

if (isset($parent->_factoryTrait)) {
$object = $parent->factory($seed);
} else {
unset($seed[key($seed)]);
$cl = $seed[0];
unset($seed[0]);
$object = new $cl(...$seed);
}
}
Expand Down

2 comments on commit 8707241

@mvorisek
Copy link
Member

@mvorisek mvorisek commented on 8707241 Mar 21, 2020

Choose a reason for hiding this comment

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

@DarkSide666 Fine, my intention was to prevent mistakes and always require the class name as the first array element as it defines the class used for everything else, so it makes sense to always defined before anything else.

@DarkSide666
Copy link
Member Author

@DarkSide666 DarkSide666 commented on 8707241 Mar 21, 2020

Choose a reason for hiding this comment

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

Well normally yes you set class first.
But what if seed is generated by merging it together from parts? For example, array_merge(['ui'=>'some css classes'], ['View']). These parts can come from different places in code and as result we never know which array element will actually be "the first one" and picking wrong one can lead to unpredictable errors.
I think checking 0 is fine and also it's BC.

Please sign in to comment.