Skip to content

Commit

Permalink
Merge pull request #193 from themehybrid/v5/php-8-container
Browse files Browse the repository at this point in the history
Try: Fix for inconsistent return with PHP8's `ReflectionParameter::getType()`.
  • Loading branch information
saas786 authored Jun 21, 2022
2 parents c72dfea + 7bc447c commit a39a125
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

You can see the changes made via the [commit log](https://github.com/justintadlock/hybrid-core/commits/master) for the latest release.

## [5.2.1] - 2022-06-21

### Changed

- Fix inconsistent reflection return type with PHP8's `ReflectionParameter::getType()`.
-
## [5.2.0] - 2019-09-03

### Added
Expand Down
52 changes: 47 additions & 5 deletions src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @package HybridCore
* @author Justin Tadlock <[email protected]>
* @copyright Copyright (c) 2008 - 2019, Justin Tadlock
* @copyright Copyright (c) 2008 - 2022, Justin Tadlock
* @link https://themehybrid.com/hybrid-core
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
Expand Down Expand Up @@ -391,13 +391,31 @@ protected function resolveDependencies( array $dependencies, array $parameters )

$args[] = $parameters[ $dependency->getName() ];

// If the parameter is a class, resolve it.
} elseif ( ! is_null( $dependency->getClass() ) ) {
continue;
}

$args[] = $this->resolve( $dependency->getClass()->getName() );
// If the parameter is a class, resolve it.
$types = $this->getReflectionTypes( $dependency );

if ( $types ) {
$resolved_type = false;

foreach ( $types as $type ) {
if ( class_exists( $type->getName() ) ) {
$args[] = $this->resolve(
$type->getName()
);
$resolved_type = true;
}
}

if ( $resolved_type ) {
continue;
}
}

// Else, use the default parameter value.
} elseif ( $dependency->isDefaultValueAvailable() ) {
if ( $dependency->isDefaultValueAvailable() ) {

$args[] = $dependency->getDefaultValue();
}
Expand All @@ -406,6 +424,30 @@ protected function resolveDependencies( array $dependencies, array $parameters )
return $args;
}

/**
* `ReflectionParameter::getType()` in PHP may return an instance of
* `ReflectionNamedType` or an `ReflectionUnionType`. The latter class's
* `getTypes()` method returns and array of the former objects. This
* method ensures that we always get an array of `ReflectionNamedType`
* objects.
*
* @since 6.1.0
* @access protected
* @param object $dependency
* @return array
*/
protected function getReflectionTypes( $dependency ) {
$types = $dependency->getType();

if ( ! $types ) {
return [];
} elseif ( class_exists( 'ReflectionUnionType' ) && $types instanceof \ReflectionUnionType ) {
return $types->getTypes();
}

return [ $types ];
}

/**
* Sets a property via `ArrayAccess`.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Application extends Container implements ApplicationContract, Bootable {
* @access public
* @var string
*/
const VERSION = '5.2.0';
const VERSION = '5.2.1';

/**
* Array of service provider objects.
Expand Down

0 comments on commit a39a125

Please sign in to comment.