Aggiornato Composer
This commit is contained in:
@@ -14,6 +14,12 @@ declare(strict_types=1);
|
||||
namespace Carbon\PHPStan;
|
||||
|
||||
use Closure;
|
||||
use InvalidArgumentException;
|
||||
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionParameter as AdapterReflectionParameter;
|
||||
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionType as AdapterReflectionType;
|
||||
use PHPStan\BetterReflection\Reflection\ReflectionClass as BetterReflectionClass;
|
||||
use PHPStan\BetterReflection\Reflection\ReflectionFunction as BetterReflectionFunction;
|
||||
use PHPStan\BetterReflection\Reflection\ReflectionParameter as BetterReflectionParameter;
|
||||
use PHPStan\Reflection\Php\BuiltinMethodReflection;
|
||||
use PHPStan\TrinaryLogic;
|
||||
use ReflectionClass;
|
||||
@@ -64,24 +70,34 @@ abstract class AbstractMacro implements BuiltinMethodReflection
|
||||
/**
|
||||
* Macro constructor.
|
||||
*
|
||||
* @param string $className
|
||||
* @phpstan-param class-string $className
|
||||
*
|
||||
* @param string $methodName
|
||||
* @param callable $macro
|
||||
* @param class-string $className
|
||||
* @param string $methodName
|
||||
* @param callable $macro
|
||||
*/
|
||||
public function __construct(string $className, string $methodName, $macro)
|
||||
{
|
||||
$this->className = $className;
|
||||
$this->methodName = $methodName;
|
||||
$this->reflectionFunction = \is_array($macro)
|
||||
$rawReflectionFunction = \is_array($macro)
|
||||
? new ReflectionMethod($macro[0], $macro[1])
|
||||
: new ReflectionFunction($macro);
|
||||
$this->parameters = $this->reflectionFunction->getParameters();
|
||||
$this->reflectionFunction = self::hasModernParser()
|
||||
? $this->getReflectionFunction($macro)
|
||||
: $rawReflectionFunction; // @codeCoverageIgnore
|
||||
$this->parameters = array_map(
|
||||
function ($parameter) {
|
||||
if ($parameter instanceof BetterReflectionParameter) {
|
||||
return new AdapterReflectionParameter($parameter);
|
||||
}
|
||||
|
||||
if ($this->reflectionFunction->isClosure()) {
|
||||
return $parameter; // @codeCoverageIgnore
|
||||
},
|
||||
$this->reflectionFunction->getParameters()
|
||||
);
|
||||
|
||||
if ($rawReflectionFunction->isClosure()) {
|
||||
try {
|
||||
$closure = $this->reflectionFunction->getClosure();
|
||||
$closure = $rawReflectionFunction->getClosure();
|
||||
$boundClosure = Closure::bind($closure, new stdClass());
|
||||
$this->static = (!$boundClosure || (new ReflectionFunction($boundClosure))->getClosureThis() === null);
|
||||
} catch (Throwable $e) {
|
||||
@@ -90,6 +106,31 @@ abstract class AbstractMacro implements BuiltinMethodReflection
|
||||
}
|
||||
}
|
||||
|
||||
private function getReflectionFunction($spec)
|
||||
{
|
||||
if (\is_array($spec) && \count($spec) === 2 && \is_string($spec[1])) {
|
||||
\assert($spec[1] !== '');
|
||||
|
||||
if (\is_object($spec[0])) {
|
||||
return BetterReflectionClass::createFromInstance($spec[0])
|
||||
->getMethod($spec[1]);
|
||||
}
|
||||
|
||||
return BetterReflectionClass::createFromName($spec[0])
|
||||
->getMethod($spec[1]);
|
||||
}
|
||||
|
||||
if (\is_string($spec)) {
|
||||
return BetterReflectionFunction::createFromName($spec);
|
||||
}
|
||||
|
||||
if ($spec instanceof Closure) {
|
||||
return BetterReflectionFunction::createFromClosure($spec);
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('Could not create reflection from the spec given'); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -175,7 +216,13 @@ abstract class AbstractMacro implements BuiltinMethodReflection
|
||||
*/
|
||||
public function getReturnType(): ?ReflectionType
|
||||
{
|
||||
return $this->reflectionFunction->getReturnType();
|
||||
$type = $this->reflectionFunction->getReturnType();
|
||||
|
||||
if ($type instanceof ReflectionType) {
|
||||
return $type; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
return self::adaptType($type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -205,18 +252,35 @@ abstract class AbstractMacro implements BuiltinMethodReflection
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getReflection(): ?ReflectionMethod
|
||||
{
|
||||
return $this->reflectionFunction instanceof ReflectionMethod
|
||||
? $this->reflectionFunction
|
||||
: null;
|
||||
}
|
||||
|
||||
public function getTentativeReturnType(): ?ReflectionType
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function returnsByReference(): TrinaryLogic
|
||||
{
|
||||
return TrinaryLogic::createNo();
|
||||
}
|
||||
|
||||
private static function adaptType($type)
|
||||
{
|
||||
$method = method_exists(AdapterReflectionType::class, 'fromTypeOrNull')
|
||||
? 'fromTypeOrNull'
|
||||
: 'fromReturnTypeOrNull'; // @codeCoverageIgnore
|
||||
|
||||
return AdapterReflectionType::$method($type);
|
||||
}
|
||||
|
||||
private static function hasModernParser(): bool
|
||||
{
|
||||
static $modernParser = null;
|
||||
|
||||
if ($modernParser !== null) {
|
||||
return $modernParser;
|
||||
}
|
||||
|
||||
$modernParser = method_exists(AdapterReflectionType::class, 'fromTypeOrNull');
|
||||
|
||||
return $modernParser;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace Carbon\PHPStan;
|
||||
|
||||
use PHPStan\BetterReflection\Reflection\Adapter;
|
||||
use PHPStan\Reflection\Php\BuiltinMethodReflection;
|
||||
use ReflectionMethod;
|
||||
|
||||
$method = new ReflectionMethod(BuiltinMethodReflection::class, 'getReflection');
|
||||
|
||||
require $method->hasReturnType() && $method->getReturnType()->getName() === Adapter\ReflectionMethod::class
|
||||
? __DIR__.'/../../../lazy/Carbon/PHPStan/AbstractMacroStatic.php'
|
||||
: __DIR__.'/../../../lazy/Carbon/PHPStan/AbstractMacroBuiltin.php';
|
||||
|
||||
$method = new ReflectionMethod(BuiltinMethodReflection::class, 'getFileName');
|
||||
|
||||
require $method->hasReturnType()
|
||||
|
||||
@@ -11,10 +11,12 @@
|
||||
|
||||
namespace Carbon\PHPStan;
|
||||
|
||||
use PHPStan\Reflection\Assertions;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use PHPStan\Reflection\MethodReflection;
|
||||
use PHPStan\Reflection\MethodsClassReflectionExtension;
|
||||
use PHPStan\Reflection\Php\PhpMethodReflectionFactory;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use PHPStan\Type\TypehintHelper;
|
||||
|
||||
/**
|
||||
@@ -38,10 +40,13 @@ final class MacroExtension implements MethodsClassReflectionExtension
|
||||
* Extension constructor.
|
||||
*
|
||||
* @param PhpMethodReflectionFactory $methodReflectionFactory
|
||||
* @param ReflectionProvider $reflectionProvider
|
||||
*/
|
||||
public function __construct(PhpMethodReflectionFactory $methodReflectionFactory)
|
||||
{
|
||||
$this->scanner = new MacroScanner();
|
||||
public function __construct(
|
||||
PhpMethodReflectionFactory $methodReflectionFactory,
|
||||
ReflectionProvider $reflectionProvider
|
||||
) {
|
||||
$this->scanner = new MacroScanner($reflectionProvider);
|
||||
$this->methodReflectionFactory = $methodReflectionFactory;
|
||||
}
|
||||
|
||||
@@ -59,6 +64,7 @@ final class MacroExtension implements MethodsClassReflectionExtension
|
||||
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
|
||||
{
|
||||
$builtinMacro = $this->scanner->getMethod($classReflection->getName(), $methodName);
|
||||
$supportAssertions = class_exists(Assertions::class);
|
||||
|
||||
return $this->methodReflectionFactory->create(
|
||||
$classReflection,
|
||||
@@ -72,7 +78,11 @@ final class MacroExtension implements MethodsClassReflectionExtension
|
||||
$builtinMacro->isDeprecated()->yes(),
|
||||
$builtinMacro->isInternal(),
|
||||
$builtinMacro->isFinal(),
|
||||
$builtinMacro->getDocComment()
|
||||
$supportAssertions ? null : $builtinMacro->getDocComment(),
|
||||
$supportAssertions ? Assertions::createEmpty() : null,
|
||||
null,
|
||||
$builtinMacro->getDocComment(),
|
||||
[]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,35 +12,55 @@
|
||||
namespace Carbon\PHPStan;
|
||||
|
||||
use Carbon\CarbonInterface;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
|
||||
final class MacroScanner
|
||||
{
|
||||
/**
|
||||
* @var \PHPStan\Reflection\ReflectionProvider
|
||||
*/
|
||||
private $reflectionProvider;
|
||||
|
||||
/**
|
||||
* MacroScanner constructor.
|
||||
*
|
||||
* @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider
|
||||
*/
|
||||
public function __construct(ReflectionProvider $reflectionProvider)
|
||||
{
|
||||
$this->reflectionProvider = $reflectionProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the given pair class-method is a Carbon macro.
|
||||
*
|
||||
* @param string $className
|
||||
* @phpstan-param class-string $className
|
||||
*
|
||||
* @param string $methodName
|
||||
* @param class-string $className
|
||||
* @param string $methodName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMethod(string $className, string $methodName): bool
|
||||
{
|
||||
return is_a($className, CarbonInterface::class, true) &&
|
||||
\is_callable([$className, 'hasMacro']) &&
|
||||
$classReflection = $this->reflectionProvider->getClass($className);
|
||||
|
||||
if (
|
||||
$classReflection->getName() !== CarbonInterface::class &&
|
||||
!$classReflection->isSubclassOf(CarbonInterface::class)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return \is_callable([$className, 'hasMacro']) &&
|
||||
$className::hasMacro($methodName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Macro for a given pair class-method.
|
||||
*
|
||||
* @param string $className
|
||||
* @phpstan-param class-string $className
|
||||
*
|
||||
* @param string $methodName
|
||||
* @param class-string $className
|
||||
* @param string $methodName
|
||||
*
|
||||
* @throws ReflectionException
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user