Skip to content

Commit

Permalink
Report descriptions of @deprecated tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 28, 2019
1 parent f5b1a45 commit 5685fe4
Show file tree
Hide file tree
Showing 47 changed files with 450 additions and 52 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"require": {
"php": "~7.1",
"nikic/php-parser": "^4.0",
"phpstan/phpstan": "^0.11.4"
"phpstan/phpstan": "^0.11.8"
},
"require-dev": {
"consistence/coding-standard": "^3.0.1",
Expand Down
18 changes: 16 additions & 2 deletions src/Rules/Deprecations/AccessDeprecatedPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,24 @@ public function processNode(Node $node, Scope $scope): array
}

if ($propertyReflection->isDeprecated()) {
$description = null;
if (method_exists($propertyReflection, 'getDeprecatedDescription')) {
$description = $propertyReflection->getDeprecatedDescription();
}

if ($description === null) {
return [sprintf(
'Access to deprecated property $%s of class %s.',
$propertyName,
$propertyReflection->getDeclaringClass()->getName()
)];
}

return [sprintf(
'Access to deprecated property $%s of class %s.',
"Access to deprecated property $%s of class %s:\n%s",
$propertyName,
$propertyReflection->getDeclaringClass()->getName()
$propertyReflection->getDeclaringClass()->getName(),
$description
)];
}
} catch (\PHPStan\Broker\ClassNotFoundException $e) {
Expand Down
18 changes: 16 additions & 2 deletions src/Rules/Deprecations/AccessDeprecatedStaticPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,24 @@ function (Type $type) use ($propertyName): bool {
}

if ($property instanceof DeprecatableReflection && $property->isDeprecated()) {
$description = null;
if (method_exists($property, 'getDeprecatedDescription')) {
$description = $property->getDeprecatedDescription();
}

if ($description === null) {
return [sprintf(
'Access to deprecated static property $%s of class %s.',
$propertyName,
$referencedClass
)];
}

return [sprintf(
'Access to deprecated static property $%s of class %s.',
"Access to deprecated static property $%s of class %s:\n%s",
$propertyName,
$referencedClass
$referencedClass,
$description
)];
}
}
Expand Down
17 changes: 15 additions & 2 deletions src/Rules/Deprecations/CallToDeprecatedFunctionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,22 @@ public function processNode(Node $node, Scope $scope): array
}

if ($function->isDeprecated()) {
$description = null;
if (method_exists($function, 'getDeprecatedDescription')) {
$description = $function->getDeprecatedDescription();
}

if ($description === null) {
return [sprintf(
'Call to deprecated function %s().',
$function->getName()
)];
}

return [sprintf(
'Call to deprecated function %s().',
$function->getName()
"Call to deprecated function %s():\n%s",
$function->getName(),
$description
)];
}

Expand Down
18 changes: 17 additions & 1 deletion src/Rules/Deprecations/CallToDeprecatedMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,29 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if ($methodReflection->isDeprecated()) {
if (!$methodReflection->isDeprecated()) {
continue;
}

$description = null;
if (method_exists($methodReflection, 'getDeprecatedDescription')) {
$description = $methodReflection->getDeprecatedDescription();
}

if ($description === null) {
return [sprintf(
'Call to deprecated method %s() of class %s.',
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName()
)];
}

return [sprintf(
"Call to deprecated method %s() of class %s:\n%s",
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName(),
$description
)];
} catch (\PHPStan\Broker\ClassNotFoundException $e) {
// Other rules will notify if the class is not found
} catch (\PHPStan\Reflection\MissingMethodFromReflectionException $e) {
Expand Down
48 changes: 38 additions & 10 deletions src/Rules/Deprecations/CallToDeprecatedStaticMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,50 @@ function (Type $type) use ($methodName): bool {
}

if ($class->isDeprecated()) {
$errors[] = sprintf(
'Call to method %s() of deprecated class %s.',
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName()
);
$classDescription = null;
if (method_exists($class, 'getDeprecatedDescription')) {
$classDescription = $class->getDeprecatedDescription();
}

if ($classDescription === null) {
$errors[] = sprintf(
'Call to method %s() of deprecated class %s.',
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName()
);
} else {
$errors[] = sprintf(
"Call to method %s() of deprecated class %s:\n%s",
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName(),
$classDescription
);
}
}

if (!$methodReflection instanceof DeprecatableReflection || !$methodReflection->isDeprecated()) {
continue;
}

$errors[] = sprintf(
'Call to deprecated method %s() of class %s.',
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName()
);
$description = null;
if (method_exists($methodReflection, 'getDeprecatedDescription')) {
$description = $methodReflection->getDeprecatedDescription();
}

if ($description === null) {
$errors[] = sprintf(
'Call to deprecated method %s() of class %s.',
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName()
);
} else {
$errors[] = sprintf(
"Call to deprecated method %s() of class %s:\n%s",
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName(),
$description
);
}
}

return $errors;
Expand Down
48 changes: 38 additions & 10 deletions src/Rules/Deprecations/FetchingClassConstOfDeprecatedClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,25 @@ function (Type $type) use ($constantName): bool {
}

if ($class->isDeprecated()) {
$errors[] = sprintf(
'Fetching class constant %s of deprecated class %s.',
$constantName,
$referencedClass
);
$classDescription = null;
if (method_exists($class, 'getDeprecatedDescription')) {
$classDescription = $class->getDeprecatedDescription();
}

if ($classDescription === null) {
$errors[] = sprintf(
'Fetching class constant %s of deprecated class %s.',
$constantName,
$referencedClass
);
} else {
$errors[] = sprintf(
"Fetching class constant %s of deprecated class %s:\n%s",
$constantName,
$referencedClass,
$classDescription
);
}
}

if (!$class->hasConstant($constantName)) {
Expand All @@ -97,11 +111,25 @@ function (Type $type) use ($constantName): bool {
continue;
}

$errors[] = sprintf(
'Fetching deprecated class constant %s of class %s.',
$constantName,
$referencedClass
);
$description = null;
if (method_exists($constantReflection, 'getDeprecatedDescription')) {
$description = $constantReflection->getDeprecatedDescription();
}

if ($description === null) {
$errors[] = sprintf(
'Fetching deprecated class constant %s of class %s.',
$constantName,
$referencedClass
);
} else {
$errors[] = sprintf(
"Fetching deprecated class constant %s of class %s:\n%s",
$constantName,
$referencedClass,
$description
);
}
}

return $errors;
Expand Down
39 changes: 30 additions & 9 deletions src/Rules/Deprecations/ImplementationOfDeprecatedInterfaceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,38 @@ public function processNode(Node $node, Scope $scope): array
$interface = $this->broker->getClass($interfaceName);

if ($interface->isDeprecated()) {
$description = null;
if (method_exists($interface, 'getDeprecatedDescription')) {
$description = $interface->getDeprecatedDescription();
}
if (!$class->isAnonymous()) {
$errors[] = sprintf(
'Class %s implements deprecated interface %s.',
$className,
$interfaceName
);
if ($description === null) {
$errors[] = sprintf(
'Class %s implements deprecated interface %s.',
$className,
$interfaceName
);
} else {
$errors[] = sprintf(
"Class %s implements deprecated interface %s:\n%s",
$className,
$interfaceName,
$description
);
}
} else {
$errors[] = sprintf(
'Anonymous class implements deprecated interface %s.',
$interfaceName
);
if ($description === null) {
$errors[] = sprintf(
'Anonymous class implements deprecated interface %s.',
$interfaceName
);
} else {
$errors[] = sprintf(
"Anonymous class implements deprecated interface %s:\n%s",
$interfaceName,
$description
);
}
}
}
} catch (\PHPStan\Broker\ClassNotFoundException $e) {
Expand Down
39 changes: 30 additions & 9 deletions src/Rules/Deprecations/InheritanceOfDeprecatedClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,40 @@ public function processNode(Node $node, Scope $scope): array

try {
$parentClass = $this->broker->getClass($parentClassName);
$description = null;
if (method_exists($parentClass, 'getDeprecatedDescription')) {
$description = $parentClass->getDeprecatedDescription();
}

if ($parentClass->isDeprecated()) {
if (!$class->isAnonymous()) {
$errors[] = sprintf(
'Class %s extends deprecated class %s.',
$className,
$parentClassName
);
if ($description === null) {
$errors[] = sprintf(
'Class %s extends deprecated class %s.',
$className,
$parentClassName
);
} else {
$errors[] = sprintf(
"Class %s extends deprecated class %s:\n%s",
$className,
$parentClassName,
$description
);
}
} else {
$errors[] = sprintf(
'Anonymous class extends deprecated class %s.',
$parentClassName
);
if ($description === null) {
$errors[] = sprintf(
'Anonymous class extends deprecated class %s.',
$parentClassName
);
} else {
$errors[] = sprintf(
"Anonymous class extends deprecated class %s:\n%s",
$parentClassName,
$description
);
}
}
}
} catch (\PHPStan\Broker\ClassNotFoundException $e) {
Expand Down
18 changes: 17 additions & 1 deletion src/Rules/Deprecations/InheritanceOfDeprecatedInterfaceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,28 @@ public function processNode(Node $node, Scope $scope): array
try {
$parentInterface = $this->broker->getClass($parentInterfaceName);

if ($parentInterface->isDeprecated()) {
if (!$parentInterface->isDeprecated()) {
continue;
}

$description = null;
if (method_exists($parentInterface, 'getDeprecatedDescription')) {
$description = $parentInterface->getDeprecatedDescription();
}

if ($description === null) {
$errors[] = sprintf(
'Interface %s extends deprecated interface %s.',
$interfaceName,
$parentInterfaceName
);
} else {
$errors[] = sprintf(
"Interface %s extends deprecated interface %s:\n%s",
$interfaceName,
$parentInterfaceName,
$description
);
}
} catch (\PHPStan\Broker\ClassNotFoundException $e) {
// Other rules will notify if the interface is not found
Expand Down
21 changes: 17 additions & 4 deletions src/Rules/Deprecations/InstantiationOfDeprecatedClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,23 @@ function (): bool {
continue;
}

$errors[] = sprintf(
'Instantiation of deprecated class %s.',
$referencedClass
);
$description = null;
if (method_exists($class, 'getDeprecatedDescription')) {
$description = $class->getDeprecatedDescription();
}

if ($description === null) {
$errors[] = sprintf(
'Instantiation of deprecated class %s.',
$referencedClass
);
} else {
$errors[] = sprintf(
"Instantiation of deprecated class %s:\n%s",
$referencedClass,
$description
);
}
}

return $errors;
Expand Down
Loading

0 comments on commit 5685fe4

Please sign in to comment.