Augmentation vers version 3.3.0

This commit is contained in:
Gauvain Boiché
2020-03-31 15:31:03 +02:00
parent d926806907
commit a1864c0414
2618 changed files with 406015 additions and 31377 deletions

View File

@@ -16,6 +16,8 @@
* and is licensed under the MIT license.
*/
declare(strict_types=1);
namespace ProxyManager\Generator;
use Zend\Code\Generator\ClassGenerator as ZendClassGenerator;
@@ -31,7 +33,7 @@ class ClassGenerator extends ZendClassGenerator
/**
* {@inheritDoc}
*/
public function setExtendedClass($extendedClass)
public function setExtendedClass($extendedClass) : parent
{
if ($extendedClass) {
$extendedClass = '\\' . trim($extendedClass, '\\');
@@ -43,7 +45,7 @@ class ClassGenerator extends ZendClassGenerator
/**
* {@inheritDoc}
*/
public function setImplementedInterfaces(array $interfaces)
public function setImplementedInterfaces(array $interfaces) : parent
{
foreach ($interfaces as & $interface) {
$interface = '\\' . trim($interface, '\\');

View File

@@ -16,6 +16,8 @@
* and is licensed under the MIT license.
*/
declare(strict_types=1);
namespace ProxyManager\Generator;
use ReflectionClass;
@@ -33,7 +35,7 @@ class MagicMethodGenerator extends MethodGenerator
* @param string $name
* @param array $parameters
*/
public function __construct(ReflectionClass $originalClass, $name, array $parameters = array())
public function __construct(ReflectionClass $originalClass, string $name, array $parameters = [])
{
parent::__construct(
$name,

View File

@@ -16,9 +16,10 @@
* and is licensed under the MIT license.
*/
declare(strict_types=1);
namespace ProxyManager\Generator;
use Zend\Code\Generator\DocBlockGenerator;
use Zend\Code\Generator\MethodGenerator as ZendMethodGenerator;
use Zend\Code\Reflection\MethodReflection;
@@ -31,131 +32,15 @@ use Zend\Code\Reflection\MethodReflection;
class MethodGenerator extends ZendMethodGenerator
{
/**
* @var bool
*/
protected $returnsReference = false;
/**
* @param boolean $returnsReference
*/
public function setReturnsReference($returnsReference)
{
$this->returnsReference = (bool) $returnsReference;
}
/**
* @return boolean
*/
public function returnsReference()
{
return $this->returnsReference;
}
/**
* @override enforces generation of \ProxyManager\Generator\MethodGenerator
*
* {@inheritDoc}
*/
public static function fromReflection(MethodReflection $reflectionMethod)
public static function fromReflection(MethodReflection $reflectionMethod) : self
{
/* @var $method self */
$method = new static();
$method = parent::fromReflection($reflectionMethod);
$method->setSourceContent($reflectionMethod->getContents(false));
$method->setSourceDirty(false);
if ($reflectionMethod->getDocComment() != '') {
$method->setDocBlock(DocBlockGenerator::fromReflection($reflectionMethod->getDocBlock()));
}
$method->setFinal($reflectionMethod->isFinal());
$method->setVisibility(self::extractVisibility($reflectionMethod));
foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
$method->setParameter(ParameterGenerator::fromReflection($reflectionParameter));
}
$method->setStatic($reflectionMethod->isStatic());
$method->setName($reflectionMethod->getName());
$method->setBody($reflectionMethod->getBody());
$method->setReturnsReference($reflectionMethod->returnsReference());
$method->setInterface(false);
return $method;
}
/**
* Retrieves the visibility for the given method reflection
*
* @param MethodReflection $reflectionMethod
*
* @return string
*/
private static function extractVisibility(MethodReflection $reflectionMethod)
{
if ($reflectionMethod->isPrivate()) {
return static::VISIBILITY_PRIVATE;
}
if ($reflectionMethod->isProtected()) {
return static::VISIBILITY_PROTECTED;
}
return static::VISIBILITY_PUBLIC;
}
/**
* @override fixes by-reference return value in zf2's method generator
*
* {@inheritDoc}
*/
public function generate()
{
$output = '';
$indent = $this->getIndentation();
if (null !== ($docBlock = $this->getDocBlock())) {
$docBlock->setIndentation($indent);
$output .= $docBlock->generate();
}
$output .= $indent . $this->generateMethodDeclaration() . self::LINE_FEED . $indent . '{' . self::LINE_FEED;
if ($this->body) {
$output .= preg_replace('#^(.+?)$#m', $indent . $indent . '$1', trim($this->body))
. self::LINE_FEED;
}
$output .= $indent . '}' . self::LINE_FEED;
return $output;
}
/**
* @return string
*/
private function generateMethodDeclaration()
{
$output = $this->generateVisibility()
. ' function '
. (($this->returnsReference()) ? '& ' : '')
. $this->getName() . '(';
$parameterOutput = array();
foreach ($this->getParameters() as $parameter) {
$parameterOutput[] = $parameter->generate();
}
return $output . implode(', ', $parameterOutput) . ')';
}
/**
* @return string
*/
private function generateVisibility()
{
return $this->isAbstract() ? 'abstract ' : (($this->isFinal()) ? 'final ' : '')
. ($this->getVisibility() . (($this->isStatic()) ? ' static' : ''));
}
}

View File

@@ -16,6 +16,8 @@
* and is licensed under the MIT license.
*/
declare(strict_types=1);
namespace ProxyManager\Generator\Util;
use ReflectionClass;
@@ -30,18 +32,11 @@ use Zend\Code\Generator\MethodGenerator;
*/
final class ClassGeneratorUtils
{
/**
* @param ReflectionClass $originalClass
* @param ClassGenerator $classGenerator
* @param MethodGenerator $generatedMethod
*
* @return void|false
*/
public static function addMethodIfNotFinal(
ReflectionClass $originalClass,
ClassGenerator $classGenerator,
MethodGenerator $generatedMethod
) {
) : bool {
$methodName = $generatedMethod->getName();
if ($originalClass->hasMethod($methodName) && $originalClass->getMethod($methodName)->isFinal()) {
@@ -49,5 +44,7 @@ final class ClassGeneratorUtils
}
$classGenerator->addMethodFromGenerator($generatedMethod);
return true;
}
}

View File

@@ -0,0 +1,41 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/
declare(strict_types=1);
namespace ProxyManager\Generator\Util;
/**
* Utility class to generate return expressions in method, given a method signature.
*
* This is required since return expressions may be forbidden by the method signature (void).
*
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*/
final class ProxiedMethodReturnExpression
{
public static function generate(string $returnedValueExpression, ?\ReflectionMethod $originalMethod) : string
{
if ($originalMethod && 'void' === (string) $originalMethod->getReturnType()) {
return $returnedValueExpression . ";\nreturn;";
}
return 'return ' . $returnedValueExpression . ';';
}
}

View File

@@ -16,6 +16,8 @@
* and is licensed under the MIT license.
*/
declare(strict_types=1);
namespace ProxyManager\Generator\Util;
/**
@@ -37,7 +39,7 @@ abstract class UniqueIdentifierGenerator
*
* @return string
*/
public static function getIdentifier($name)
public static function getIdentifier(string $name) : string
{
return str_replace(
'.',