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

[Feature] Implement parameter widening feature for generated code (#380) #383

Merged
merged 1 commit into from
Jan 8, 2018
Merged
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
7 changes: 7 additions & 0 deletions src/Aop/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ interface Features
* This flag is usable for read-only file systems (GAE, phar, etc)
*/
const PREBUILT_CACHE = 64;

/**
* Enables usage of parameter widening for PHP>=7.2.0
*
* @see https://wiki.php.net/rfc/parameter-no-type-variance
*/
const PARAMETER_WIDENING = 128;
}
15 changes: 13 additions & 2 deletions src/Instrument/Transformer/WeavingTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Go\Aop\Advisor;
use Go\Aop\Aspect;
use Go\Aop\Features;
use Go\Aop\Framework\AbstractJoinpoint;
use Go\Core\AdviceMatcher;
use Go\Core\AspectContainer;
Expand All @@ -38,6 +39,13 @@ class WeavingTransformer extends BaseSourceTransformer
*/
protected $adviceMatcher;

/**
* Should we use parameter widening for our decorators
*
* @var bool
*/
protected $useParameterWidening = false;

/**
* @var CachePathManager
*/
Expand Down Expand Up @@ -68,6 +76,9 @@ public function __construct(
$this->adviceMatcher = $adviceMatcher;
$this->cachePathManager = $cachePathManager;
$this->aspectLoader = $loader;
if (PHP_VERSION_ID >= 70200) {
$this->useParameterWidening = $kernel->hasFeature(Features::PARAMETER_WIDENING);
}
}

/**
Expand Down Expand Up @@ -144,8 +155,8 @@ private function processSingleClass(array $advisors, StreamMetaData $metadata, R

// Prepare child Aop proxy
$child = $class->isTrait()
? new TraitProxy($class, $advices)
: new ClassProxy($class, $advices);
? new TraitProxy($class, $advices, $this->useParameterWidening)
: new ClassProxy($class, $advices, $this->useParameterWidening);

// Set new parent name instead of original
$child->setParentName($newClassName);
Expand Down
17 changes: 14 additions & 3 deletions src/Proxy/AbstractProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,25 @@ abstract class AbstractProxy
*/
protected static $staticLsbExpression = 'static::class';

/**
* Whether or not proxy should use parameter widening
*
* @see https://wiki.php.net/rfc/parameter-no-type-variance
*
* @var bool
*/
private $useParameterWidening;

/**
* Constructs an abstract proxy class
*
* @param array $advices List of advices
* @param bool $useParameterWidening Should proxy use parameter widening feature
*/
public function __construct(array $advices = [])
public function __construct(array $advices = [], bool $useParameterWidening = false)
{
$this->advices = $this->flattenAdvices($advices);
$this->advices = $this->flattenAdvices($advices);
$this->useParameterWidening = $useParameterWidening;
}

/**
Expand Down Expand Up @@ -105,7 +116,7 @@ protected function getParameterCode(ReflectionParameter $parameter): string
{
$type = '';
$reflectionType = $parameter->getType();
if ($reflectionType) {
if ($reflectionType !== null && $this->useParameterWidening === false) {
$nullablePrefix = (PHP_VERSION_ID >= 70100 && $reflectionType->allowsNull()) ? '?' : '';
$nsPrefix = $reflectionType->isBuiltin() ? '' : '\\';
$type = $nullablePrefix . $nsPrefix . ltrim((string) $reflectionType, '\\');
Expand Down
5 changes: 3 additions & 2 deletions src/Proxy/ClassProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ class ClassProxy extends AbstractProxy
*
* @param ReflectionClass $parent Parent class reflection
* @param array|Advice[][] $classAdvices List of advices for class
* @param bool $useParameterWidening Enables usage of parameter widening feature
*
* @throws \InvalidArgumentException if there are unknown type of advices
*/
public function __construct(ReflectionClass $parent, array $classAdvices)
public function __construct(ReflectionClass $parent, array $classAdvices, bool $useParameterWidening)
{
parent::__construct($classAdvices);
parent::__construct($classAdvices, $useParameterWidening);

$this->class = $parent;
$this->name = $parent->getShortName();
Expand Down