Skip to content

Commit

Permalink
Merge pull request #572 from Roave/fix/align-adapter-types-to-ext-ref…
Browse files Browse the repository at this point in the history
…lection-collection-ish-method-types

Align adapter types to ext reflection collection-ish method types
  • Loading branch information
Ocramius committed May 7, 2020
2 parents cf9ff24 + 8142147 commit 23860f1
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 71 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"doctrine/coding-standard": "^7.0.2",
"phpstan/phpstan": "^0.12.19",
"phpunit/phpunit": "^8.5.4",
"vimeo/psalm": "3.10.1"
"vimeo/psalm": "3.11.2"
},
"autoload": {
"psr-4": {
Expand Down
85 changes: 47 additions & 38 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 29 additions & 15 deletions src/Reflection/Adapter/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
use Roave\BetterReflection\Reflection\ReflectionClassConstant as BetterReflectionClassConstant;
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionObject as BetterReflectionObject;
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
use function array_combine;
use function array_map;
use function array_values;
use function assert;
use function func_num_args;
use function is_array;
use function is_object;
use function is_string;
use function sprintf;
Expand Down Expand Up @@ -227,14 +231,9 @@ public function getProperty($name)
*/
public function getProperties($filter = null)
{
$properties = $this->betterReflectionClass->getProperties();

$wrappedProperties = [];
foreach ($properties as $key => $property) {
$wrappedProperties[$key] = new ReflectionProperty($property);
}

return $wrappedProperties;
return array_values(array_map(static function (BetterReflectionProperty $property) : ReflectionProperty {
return new ReflectionProperty($property);
}, $this->betterReflectionClass->getProperties()));
}

/**
Expand Down Expand Up @@ -278,9 +277,9 @@ public function getReflectionConstant($name)
*/
public function getReflectionConstants()
{
return array_map(static function (BetterReflectionClassConstant $betterConstant) : ReflectionClassConstant {
return array_values(array_map(static function (BetterReflectionClassConstant $betterConstant) : ReflectionClassConstant {
return new ReflectionClassConstant($betterConstant);
}, $this->betterReflectionClass->getReflectionConstants());
}, $this->betterReflectionClass->getReflectionConstants()));
}

/**
Expand Down Expand Up @@ -323,12 +322,27 @@ public function getTraits()
{
$traits = $this->betterReflectionClass->getTraits();

$wrappedTraits = [];
foreach ($traits as $key => $trait) {
$wrappedTraits[$key] = new self($trait);
}
/** @psalm-var array<trait-string> $traitNames */
$traitNames = array_map(static function (BetterReflectionClass $trait) : string {
return $trait->getName();
}, $traits);

$traitsByName = array_combine(
$traitNames,
array_map(static function (BetterReflectionClass $trait) : self {
return new self($trait);
}, $traits)
);

assert(
is_array($traitsByName),
sprintf(
'Could not create an array<trait-string, ReflectionClass> for class "%s"',
$this->betterReflectionClass->getName()
)
);

return $wrappedTraits;
return $traitsByName;
}

/**
Expand Down
Loading

0 comments on commit 23860f1

Please sign in to comment.