-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set properties autowired with @required as initialized
- Loading branch information
1 parent
4f984e5
commit cee621e
Showing
8 changed files
with
254 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Symfony; | ||
|
||
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass; | ||
use PHPStan\Reflection\AdditionalConstructorsExtension; | ||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Reflection\Php\PhpPropertyReflection; | ||
use PHPStan\Reflection\PropertyReflection; | ||
use PHPStan\Rules\Properties\ReadWritePropertiesExtension; | ||
use PHPStan\Type\FileTypeMapper; | ||
use Symfony\Contracts\Service\Attribute\Required; | ||
use function count; | ||
|
||
class RequiredAutowiringExtension implements ReadWritePropertiesExtension, AdditionalConstructorsExtension | ||
{ | ||
|
||
/** @var FileTypeMapper */ | ||
private $fileTypeMapper; | ||
|
||
public function __construct(FileTypeMapper $fileTypeMapper) | ||
{ | ||
$this->fileTypeMapper = $fileTypeMapper; | ||
} | ||
|
||
public function isAlwaysRead(PropertyReflection $property, string $propertyName): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function isAlwaysWritten(PropertyReflection $property, string $propertyName): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function isInitialized(PropertyReflection $property, string $propertyName): bool | ||
{ | ||
// If the property is public, check for @required on the property itself | ||
if (!$property->isPublic()) { | ||
return false; | ||
} | ||
|
||
if ($property->getDocComment() !== null && $this->isRequiredFromDocComment($property->getDocComment())) { | ||
return true; | ||
} | ||
|
||
// Check for the attribute version | ||
if ($property instanceof PhpPropertyReflection && count($property->getNativeReflection()->getAttributes(Required::class)) > 0) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function getAdditionalConstructors(ClassReflection $classReflection): array | ||
{ | ||
$additionalConstructors = []; | ||
$nativeReflection = $classReflection->getNativeReflection(); | ||
|
||
foreach ($nativeReflection->getMethods() as $method) { | ||
if (!$method->isPublic()) { | ||
continue; | ||
} | ||
|
||
if ($method->getDocComment() !== false && $this->isRequiredFromDocComment($method->getDocComment())) { | ||
$additionalConstructors[] = $method->getName(); | ||
} | ||
|
||
if (count($method->getAttributes(Required::class)) === 0) { | ||
continue; | ||
} | ||
|
||
$additionalConstructors[] = $method->getName(); | ||
} | ||
|
||
return $additionalConstructors; | ||
} | ||
|
||
private function isRequiredFromDocComment(string $docComment): bool | ||
{ | ||
$phpDoc = $this->fileTypeMapper->getResolvedPhpDoc(null, null, null, null, $docComment); | ||
|
||
foreach ($phpDoc->getPhpDocNodes() as $node) { | ||
// @required tag is available, meaning this property is always initialized | ||
if (count($node->getTagsByName('@required')) > 0) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Symfony; | ||
|
||
use PHPStan\Reflection\AdditionalConstructorsExtension; | ||
use PHPStan\Rules\Properties\UninitializedPropertyRule; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use Symfony\Contracts\Service\Attribute\Required; | ||
use function class_exists; | ||
|
||
/** | ||
* @extends RuleTestCase<UninitializedPropertyRule> | ||
*/ | ||
final class RequiredAutowiringExtensionTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$container = self::getContainer(); | ||
$container->getServicesByTag(AdditionalConstructorsExtension::EXTENSION_TAG); | ||
|
||
return $container->getByType(UninitializedPropertyRule::class); | ||
} | ||
|
||
public function testRequiredAnnotations(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/required-annotations.php'], [ | ||
[ | ||
'Class RequiredAnnotationTest\TestAnnotations has an uninitialized property $three. Give it default value or assign it in the constructor.', | ||
12, | ||
], | ||
[ | ||
'Class RequiredAnnotationTest\TestAnnotations has an uninitialized property $four. Give it default value or assign it in the constructor.', | ||
14, | ||
], | ||
]); | ||
} | ||
|
||
public function testRequiredAttributes(): void | ||
{ | ||
if (!class_exists(Required::class)) { | ||
self::markTestSkipped('Required symfony/[email protected] or higher is not installed'); | ||
} | ||
|
||
$this->analyse([__DIR__ . '/data/required-attributes.php'], [ | ||
[ | ||
'Class RequiredAttributesTest\TestAttributes has an uninitialized property $three. Give it default value or assign it in the constructor.', | ||
14, | ||
], | ||
[ | ||
'Class RequiredAttributesTest\TestAttributes has an uninitialized property $four. Give it default value or assign it in the constructor.', | ||
16, | ||
], | ||
]); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/required-autowiring-config.neon', | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php // lint >= 7.4 | ||
|
||
namespace RequiredAnnotationTest; | ||
|
||
class TestAnnotations | ||
{ | ||
/** @required */ | ||
public string $one; | ||
|
||
private string $two; | ||
|
||
public string $three; | ||
|
||
private string $four; | ||
|
||
/** | ||
* @required | ||
*/ | ||
public function setTwo(int $two): void | ||
{ | ||
$this->two = $two; | ||
} | ||
|
||
public function getTwo(): int | ||
{ | ||
return $this->two; | ||
} | ||
|
||
public function setFour(int $four): void | ||
{ | ||
$this->four = $four; | ||
} | ||
|
||
public function getFour(): int | ||
{ | ||
return $this->four; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php // lint >= 8.0 | ||
|
||
namespace RequiredAttributesTest; | ||
|
||
use Symfony\Contracts\Service\Attribute\Required; | ||
|
||
class TestAttributes | ||
{ | ||
#[Required] | ||
public string $one; | ||
|
||
private string $two; | ||
|
||
public string $three; | ||
|
||
private string $four; | ||
|
||
#[Required] | ||
public function setTwo(int $two): void | ||
{ | ||
$this->two = $two; | ||
} | ||
|
||
public function getTwo(): int | ||
{ | ||
return $this->two; | ||
} | ||
|
||
public function setFour(int $four): void | ||
{ | ||
$this->four = $four; | ||
} | ||
|
||
public function getFour(): int | ||
{ | ||
return $this->four; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
- | ||
class: PHPStan\Symfony\RequiredAutowiringExtension | ||
tags: | ||
- phpstan.properties.readWriteExtension | ||
- phpstan.additionalConstructorsExtension |