-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from themehybrid/v5/php-8-container
Try: Fix for inconsistent return with PHP8's `ReflectionParameter::getType()`.
- Loading branch information
Showing
3 changed files
with
54 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
*/ | ||
|
@@ -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(); | ||
} | ||
|
@@ -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`. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters