This commit is contained in:
1 parent
f7c532a648
commit
640bd67556
5801 files changed
+484532
-55857
No files matched your search
+99
-34
@@ -18,8 +18,11 @@ use Mockery\MockInterface;
|
||||
use Phake\IMock;
|
||||
use PHPUnit\Framework\MockObject\Matcher\StatelessInvocation;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\MockObject\Stub;
|
||||
use Prophecy\Prophecy\ProphecySubjectInterface;
|
||||
use ProxyManager\Proxy\ProxyInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
use Symfony\Component\DependencyInjection\Argument\LazyClosure;
|
||||
use Symfony\Component\ErrorHandler\Internal\TentativeTypes;
|
||||
use Symfony\Component\VarExporter\LazyObjectInterface;
|
||||
|
||||
@@ -68,12 +71,14 @@ class DebugClassLoader
|
||||
'iterable' => 'iterable',
|
||||
'object' => 'object',
|
||||
'string' => 'string',
|
||||
'non-empty-string' => 'string',
|
||||
'self' => 'self',
|
||||
'parent' => 'parent',
|
||||
'mixed' => 'mixed',
|
||||
'static' => 'static',
|
||||
'$this' => 'static',
|
||||
'list' => 'array',
|
||||
'non-empty-list' => 'array',
|
||||
'class-string' => 'string',
|
||||
'never' => 'never',
|
||||
];
|
||||
@@ -129,6 +134,21 @@ class DebugClassLoader
|
||||
private static array $internalMethods = [];
|
||||
private static array $annotatedParameters = [];
|
||||
private static array $darwinCache = ['/' => ['/', []]];
|
||||
/**
|
||||
* @var array<string, list<array{0: string, 1: bool, 2: string, 3: string, 4: string|null}>>
|
||||
*
|
||||
* Maps an interface FQCN (or an abstract class accumulating entries from its interfaces) to the list of
|
||||
* "@method" annotations declared on it. For interfaces, the entry is populated directly by parsing the
|
||||
* annotations from the interface's docblock. For abstract classes, the information from all implemented
|
||||
* interfaces is merged together, so that the check can later be applied to the first concrete subclass.
|
||||
*
|
||||
* Each entry is a tuple of:
|
||||
* [0] string $interface - FQCN of the interface that carries the "@method" annotation
|
||||
* [1] bool $static - whether the method is declared static
|
||||
* [2] string $returnType - return type from the annotation, or '' if absent
|
||||
* [3] string $name - method name plus its parameter signature, e.g. "foo($arg, int $n)"
|
||||
* [4] string|null $description - description text (period-normalised), or null if absent
|
||||
*/
|
||||
private static array $method = [];
|
||||
private static array $returnTypes = [];
|
||||
private static array $methodTraits = [];
|
||||
@@ -185,7 +205,7 @@ class DebugClassLoader
|
||||
{
|
||||
// Ensures we don't hit https://bugs.php.net/42098
|
||||
class_exists(ErrorHandler::class);
|
||||
class_exists(\Psr\Log\LogLevel::class);
|
||||
class_exists(LogLevel::class);
|
||||
|
||||
if (!\is_array($functions = spl_autoload_functions())) {
|
||||
return;
|
||||
@@ -256,6 +276,7 @@ class DebugClassLoader
|
||||
|
||||
for (; $i < \count($symbols); ++$i) {
|
||||
if (!is_subclass_of($symbols[$i], MockObject::class)
|
||||
&& !is_subclass_of($symbols[$i], Stub::class)
|
||||
&& !is_subclass_of($symbols[$i], ProphecySubjectInterface::class)
|
||||
&& !is_subclass_of($symbols[$i], Proxy::class)
|
||||
&& !is_subclass_of($symbols[$i], ProxyInterface::class)
|
||||
@@ -263,6 +284,7 @@ class DebugClassLoader
|
||||
&& !is_subclass_of($symbols[$i], LegacyProxy::class)
|
||||
&& !is_subclass_of($symbols[$i], MockInterface::class)
|
||||
&& !is_subclass_of($symbols[$i], IMock::class)
|
||||
&& !(is_subclass_of($symbols[$i], LazyClosure::class) && str_contains($symbols[$i], "@anonymous\0"))
|
||||
) {
|
||||
$loader->checkClass($symbols[$i]);
|
||||
}
|
||||
@@ -332,7 +354,7 @@ class DebugClassLoader
|
||||
$name = $refl->getName();
|
||||
|
||||
if ($name !== $class && 0 === strcasecmp($name, $class)) {
|
||||
throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: "%s" vs "%s".', $class, $name));
|
||||
throw new \RuntimeException(\sprintf('Case mismatch between loaded and declared class names: "%s" vs "%s".', $class, $name));
|
||||
}
|
||||
|
||||
$deprecations = $this->checkAnnotations($refl, $name);
|
||||
@@ -348,14 +370,14 @@ class DebugClassLoader
|
||||
|
||||
if (!$exists) {
|
||||
if (str_contains($class, '/')) {
|
||||
throw new \RuntimeException(sprintf('Trying to autoload a class with an invalid name "%s". Be careful that the namespace separator is "\" in PHP, not "/".', $class));
|
||||
throw new \RuntimeException(\sprintf('Trying to autoload a class with an invalid name "%s". Be careful that the namespace separator is "\" in PHP, not "/".', $class));
|
||||
}
|
||||
|
||||
throw new \RuntimeException(sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.', $class, $file));
|
||||
throw new \RuntimeException(\sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.', $class, $file));
|
||||
}
|
||||
|
||||
if (self::$caseCheck && $message = $this->checkCase($refl, $file, $class)) {
|
||||
throw new \RuntimeException(sprintf('Case mismatch between class and real file names: "%s" vs "%s" in "%s".', $message[0], $message[1], $message[2]));
|
||||
throw new \RuntimeException(\sprintf('Case mismatch between class and real file names: "%s" vs "%s" in "%s".', $message[0], $message[1], $message[2]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -373,7 +395,7 @@ class DebugClassLoader
|
||||
|
||||
// Don't trigger deprecations for classes in the same vendor
|
||||
if ($class !== $className) {
|
||||
$vendor = preg_match('/^namespace ([^;\\\\\s]++)[;\\\\]/m', @file_get_contents($refl->getFileName()), $vendor) ? $vendor[1].'\\' : '';
|
||||
$vendor = $refl->getFileName() && preg_match('/^namespace ([^;\\\\\s]++)[;\\\\]/m', @file_get_contents($refl->getFileName()) ?: '', $vendor) ? $vendor[1].'\\' : '';
|
||||
$vendorLen = \strlen($vendor);
|
||||
} elseif (2 > $vendorLen = 1 + (strpos($class, '\\') ?: strpos($class, '_'))) {
|
||||
$vendorLen = 0;
|
||||
@@ -398,6 +420,14 @@ class DebugClassLoader
|
||||
|
||||
if ($refl->isInterface() && isset($doc['method'])) {
|
||||
foreach ($doc['method'] as $name => [$static, $returnType, $signature, $description]) {
|
||||
if ($refl->hasMethod($static ? '__callStatic' : '__call')) {
|
||||
// When the interface has "virtual" @method declarations but at the same time contains a __call/__callStatic magic method,
|
||||
// do not trigger a deprecation notice. This is to address special use cases like in Predis' ClientInterface where the
|
||||
// "@method" annotations never intend to actually add the method to the interface, but are used to document the "virtual"
|
||||
// API provided by the interface through the technical implementation of magic calls. This might cause false negatives
|
||||
// (missing notices) in the case that such interfaces are later amended with actual (real) methods.
|
||||
continue;
|
||||
}
|
||||
self::$method[$class][] = [$class, $static, $returnType, $name.$signature, $description];
|
||||
|
||||
if ('' !== $returnType) {
|
||||
@@ -416,10 +446,19 @@ class DebugClassLoader
|
||||
}
|
||||
|
||||
if (isset(self::$final[$parent])) {
|
||||
$deprecations[] = sprintf('The "%s" class is considered final%s It may change without further notice as of its next major version. You should not extend it from "%s".', $parent, self::$final[$parent], $className);
|
||||
$deprecations[] = \sprintf('The "%s" class is considered final%s It may change without further notice as of its next major version. You should not extend it from "%s".', $parent, self::$final[$parent], $className);
|
||||
}
|
||||
}
|
||||
|
||||
// When the parent is a concrete class, we will trigger deprecation notices to make it aware that it needs
|
||||
// to add the new methods announced with @method. The parent will have to provide all those methods.
|
||||
// For child classes this means they will not need to deal with @method coming from any of the interfaces
|
||||
// the parent implements.
|
||||
// Put those interfaces that we can ignore into $parentInterfaces.
|
||||
// The ternary makes use of the fact that abstract parent classes will accumulate the methods in self::$method,
|
||||
// so !isset(self::$method[$parent]) indicates a concrete parent class.
|
||||
$parentInterfaces = ($parent && !isset(self::$method[$parent])) ? class_implements($parent, false) : [];
|
||||
|
||||
// Detect if the parent is annotated
|
||||
foreach ($parentAndOwnInterfaces + class_uses($class, false) as $use) {
|
||||
if (!isset(self::$checkedClasses[$use])) {
|
||||
@@ -429,19 +468,21 @@ class DebugClassLoader
|
||||
$type = class_exists($class, false) ? 'class' : (interface_exists($class, false) ? 'interface' : 'trait');
|
||||
$verb = class_exists($use, false) || interface_exists($class, false) ? 'extends' : (interface_exists($use, false) ? 'implements' : 'uses');
|
||||
|
||||
$deprecations[] = sprintf('The "%s" %s %s "%s" that is deprecated%s', $className, $type, $verb, $use, self::$deprecated[$use]);
|
||||
$deprecations[] = \sprintf('The "%s" %s %s "%s" that is deprecated%s', $className, $type, $verb, $use, self::$deprecated[$use]);
|
||||
}
|
||||
if (isset(self::$internal[$use]) && strncmp($vendor, str_replace('_', '\\', $use), $vendorLen)) {
|
||||
$deprecations[] = sprintf('The "%s" %s is considered internal%s It may change without further notice. You should not use it from "%s".', $use, class_exists($use, false) ? 'class' : (interface_exists($use, false) ? 'interface' : 'trait'), self::$internal[$use], $className);
|
||||
$deprecations[] = \sprintf('The "%s" %s is considered internal%s It may change without further notice. You should not use it from "%s".', $use, class_exists($use, false) ? 'class' : (interface_exists($use, false) ? 'interface' : 'trait'), self::$internal[$use], $className);
|
||||
}
|
||||
if (isset(self::$method[$use])) {
|
||||
if ($refl->isAbstract()) {
|
||||
if ($refl->isAbstract() || $refl->isInterface()) {
|
||||
// Abstract classes and interfaces inherit @method from interfaces they
|
||||
// implement directly or through inheritance.
|
||||
if (isset(self::$method[$class])) {
|
||||
self::$method[$class] = array_merge(self::$method[$class], self::$method[$use]);
|
||||
} else {
|
||||
self::$method[$class] = self::$method[$use];
|
||||
}
|
||||
} elseif (!$refl->isInterface()) {
|
||||
} else {
|
||||
if (!strncmp($vendor, str_replace('_', '\\', $use), $vendorLen)
|
||||
&& str_starts_with($className, 'Symfony\\')
|
||||
&& (!class_exists(InstalledVersions::class)
|
||||
@@ -450,15 +491,15 @@ class DebugClassLoader
|
||||
// skip "same vendor" @method deprecations for Symfony\* classes unless symfony/symfony is being tested
|
||||
continue;
|
||||
}
|
||||
$hasCall = $refl->hasMethod('__call');
|
||||
$hasStaticCall = $refl->hasMethod('__callStatic');
|
||||
foreach (self::$method[$use] as [$interface, $static, $returnType, $name, $description]) {
|
||||
if ($static ? $hasStaticCall : $hasCall) {
|
||||
if (isset($parentInterfaces[$interface])) {
|
||||
// The @method annotation comes from an interface that has already been implemented by a concrete parent class,
|
||||
// so we can ignore it here.
|
||||
continue;
|
||||
}
|
||||
$realName = substr($name, 0, strpos($name, '('));
|
||||
if (!$refl->hasMethod($realName) || !($methodRefl = $refl->getMethod($realName))->isPublic() || ($static && !$methodRefl->isStatic()) || (!$static && $methodRefl->isStatic())) {
|
||||
$deprecations[] = sprintf('Class "%s" should implement method "%s::%s%s"%s', $className, ($static ? 'static ' : '').$interface, $name, $returnType ? ': '.$returnType : '', null === $description ? '.' : ': '.$description);
|
||||
if (!$refl->hasMethod($realName) || !($methodRefl = $refl->getMethod($realName))->isPublic() || ($static xor $methodRefl->isStatic())) {
|
||||
$deprecations[] = \sprintf('Class "%s" should implement method "%s::%s%s"%s', $className, ($static ? 'static ' : '').$interface, $name, $returnType ? ': '.$returnType : '', null === $description ? '.' : ': '.$description);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -522,13 +563,13 @@ class DebugClassLoader
|
||||
|
||||
if ($parent && isset(self::$finalMethods[$parent][$method->name])) {
|
||||
[$declaringClass, $message] = self::$finalMethods[$parent][$method->name];
|
||||
$deprecations[] = sprintf('The "%s::%s()" method is considered final%s It may change without further notice as of its next major version. You should not extend it from "%s".', $declaringClass, $method->name, $message, $className);
|
||||
$deprecations[] = \sprintf('The "%s::%s()" method is considered final%s It may change without further notice as of its next major version. You should not extend it from "%s".', $declaringClass, $method->name, $message, $className);
|
||||
}
|
||||
|
||||
if (isset(self::$internalMethods[$class][$method->name])) {
|
||||
[$declaringClass, $message] = self::$internalMethods[$class][$method->name];
|
||||
if (strncmp($ns, $declaringClass, $len)) {
|
||||
$deprecations[] = sprintf('The "%s::%s()" method is considered internal%s It may change without further notice. You should not extend it from "%s".', $declaringClass, $method->name, $message, $className);
|
||||
$deprecations[] = \sprintf('The "%s::%s()" method is considered internal%s It may change without further notice. You should not extend it from "%s".', $declaringClass, $method->name, $message, $className);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -547,7 +588,7 @@ class DebugClassLoader
|
||||
|
||||
foreach (self::$annotatedParameters[$class][$method->name] as $parameterName => $deprecation) {
|
||||
if (!isset($definedParameters[$parameterName]) && !isset($doc['param'][$parameterName])) {
|
||||
$deprecations[] = sprintf($deprecation, $className);
|
||||
$deprecations[] = \sprintf($deprecation, $className);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -583,7 +624,7 @@ class DebugClassLoader
|
||||
if ('docblock' === $this->patchTypes['force']) {
|
||||
$this->patchMethod($method, $returnType, $declaringFile, $normalizedType);
|
||||
} elseif ('' !== $declaringClass && $this->patchTypes['deprecations']) {
|
||||
$deprecations[] = sprintf('Method "%s::%s()" might add "%s" as a native return type declaration in the future. Do the same in %s "%s" now to avoid errors or add an explicit @return annotation to suppress this message.', $declaringClass, $method->name, $normalizedType, interface_exists($declaringClass) ? 'implementation' : 'child class', $className);
|
||||
$deprecations[] = \sprintf('Method "%s::%s()" might add "%s" as a native return type declaration in the future. Do the same in %s "%s" now to avoid errors or add an explicit @return annotation to suppress this message.', $declaringClass, $method->name, $normalizedType, interface_exists($declaringClass) ? 'implementation' : 'child class', $className);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -632,7 +673,7 @@ class DebugClassLoader
|
||||
}
|
||||
foreach ($doc['param'] as $parameterName => $parameterType) {
|
||||
if (!isset($definedParameters[$parameterName])) {
|
||||
self::$annotatedParameters[$class][$method->name][$parameterName] = sprintf('The "%%s::%s()" method will require a new "%s$%s" argument in the next major version of its %s "%s", not defining it is deprecated.', $method->name, $parameterType ? $parameterType.' ' : '', $parameterName, interface_exists($className) ? 'interface' : 'parent class', $className);
|
||||
self::$annotatedParameters[$class][$method->name][$parameterName] = \sprintf('The "%%s::%s()" method will require a new "%s$%s" argument in the next major version of its %s "%s", not defining it is deprecated.', $method->name, $parameterType ? $parameterType.' ' : '', $parameterName, interface_exists($className) ? 'interface' : 'parent class', $className);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -652,7 +693,7 @@ class DebugClassLoader
|
||||
foreach ($parentAndOwnInterfaces as $use) {
|
||||
if (isset(self::${$type}[$use][$r->name]) && !isset($doc['deprecated']) && ('finalConstants' === $type || substr($use, 0, strrpos($use, '\\')) !== substr($use, 0, strrpos($class, '\\')))) {
|
||||
$msg = 'finalConstants' === $type ? '%s" constant' : '$%s" property';
|
||||
$deprecations[] = sprintf('The "%s::'.$msg.' is considered final. You should not override it in "%s".', self::${$type}[$use][$r->name], $r->name, $class);
|
||||
$deprecations[] = \sprintf('The "%s::'.$msg.' is considered final. You should not override it in "%s".', self::${$type}[$use][$r->name], $r->name, $class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -840,8 +881,8 @@ class DebugClassLoader
|
||||
$iterable = $object = true;
|
||||
foreach ($typesMap as $n => $t) {
|
||||
if ('null' !== $n) {
|
||||
$iterable = $iterable && (\in_array($n, ['array', 'iterable']) || str_contains($n, 'Iterator'));
|
||||
$object = $object && (\in_array($n, ['callable', 'object', '$this', 'static']) || !isset(self::SPECIAL_RETURN_TYPES[$n]));
|
||||
$iterable = $iterable && (\in_array($n, ['array', 'iterable'], true) || str_contains($n, 'Iterator'));
|
||||
$object = $object && (\in_array($n, ['callable', 'object', '$this', 'static'], true) || !isset(self::SPECIAL_RETURN_TYPES[$n]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -849,6 +890,30 @@ class DebugClassLoader
|
||||
$docTypes = [];
|
||||
|
||||
foreach ($typesMap as $n => $t) {
|
||||
if (str_contains($n, '::')) {
|
||||
[$definingClass, $constantName] = explode('::', $n, 2);
|
||||
$definingClass = match ($definingClass) {
|
||||
'self', 'static', 'parent' => $class,
|
||||
default => $definingClass,
|
||||
};
|
||||
|
||||
if (!\defined($definingClass.'::'.$constantName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$constant = new \ReflectionClassConstant($definingClass, $constantName);
|
||||
|
||||
if (\PHP_VERSION_ID >= 80300 && $constantType = $constant->getType()) {
|
||||
if ($constantType instanceof \ReflectionNamedType) {
|
||||
$n = $constantType->getName();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
$n = \gettype($constant->getValue());
|
||||
}
|
||||
}
|
||||
|
||||
if ('null' === $n) {
|
||||
$nullable = true;
|
||||
continue;
|
||||
@@ -872,7 +937,7 @@ class DebugClassLoader
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isset($phpTypes[''])) {
|
||||
if (!isset($phpTypes['']) && !\in_array($n, $phpTypes, true)) {
|
||||
$phpTypes[] = $n;
|
||||
}
|
||||
}
|
||||
@@ -893,8 +958,8 @@ class DebugClassLoader
|
||||
}
|
||||
}
|
||||
|
||||
$phpType = sprintf($nullable ? (1 < \count($phpTypes) ? '%s|null' : '?%s') : '%s', implode($glue, $phpTypes));
|
||||
$docType = sprintf($nullable ? '%s|null' : '%s', implode($glue, $docTypes));
|
||||
$phpType = \sprintf($nullable ? (1 < \count($phpTypes) ? '%s|null' : '?%s') : '%s', implode($glue, $phpTypes));
|
||||
$docType = \sprintf($nullable ? '%s|null' : '%s', implode($glue, $docTypes));
|
||||
|
||||
self::$returnTypes[$class][$method] = [$phpType, $docType, $class, $filename];
|
||||
}
|
||||
@@ -1026,7 +1091,7 @@ class DebugClassLoader
|
||||
++$fileOffset;
|
||||
}
|
||||
|
||||
$returnType[$i] = null !== $format ? sprintf($format, $alias) : $alias;
|
||||
$returnType[$i] = null !== $format ? \sprintf($format, $alias) : $alias;
|
||||
}
|
||||
|
||||
if ('docblock' === $this->patchTypes['force'] || ('object' === $normalizedType && '7.1' === $this->patchTypes['php'])) {
|
||||
@@ -1040,11 +1105,11 @@ class DebugClassLoader
|
||||
$code[$startLine] = " * @return $returnType\n".$code[$startLine];
|
||||
} else {
|
||||
$code[$startLine] .= <<<EOTXT
|
||||
/**
|
||||
* @return $returnType
|
||||
*/
|
||||
/**
|
||||
* @return $returnType
|
||||
*/
|
||||
|
||||
EOTXT;
|
||||
EOTXT;
|
||||
}
|
||||
|
||||
$fileOffset += substr_count($code[$startLine], "\n") - 1;
|
||||
@@ -1135,7 +1200,7 @@ EOTXT;
|
||||
$braces = 0;
|
||||
for (; $i < $end; ++$i) {
|
||||
if (!$inClosure) {
|
||||
$inClosure = false !== strpos($code[$i], 'function (');
|
||||
$inClosure = str_contains($code[$i], 'function (');
|
||||
}
|
||||
|
||||
if ($inClosure) {
|
||||
@@ -1214,7 +1279,7 @@ EOTXT;
|
||||
$static = 'static' === $parts[0];
|
||||
|
||||
for ($i = $static ? 2 : 0; null !== $p = $parts[$i] ?? null; $i += 2) {
|
||||
if (\in_array($p, ['', '|', '&', 'callable'], true) || \in_array(substr($returnType, -1), ['|', '&'], true)) {
|
||||
if (\in_array($p, ['', 'callable'], true) || \in_array(substr($returnType, -1), ['|', '&'], true) || \in_array($p[0], ['|', '&'], true)) {
|
||||
$returnType .= trim($parts[$i - 1] ?? '').$p;
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user