Skip to content

Add extension to add additional constructors through code #2348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Reflection/AdditionalConstructorsExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection;

/**
* This is the extension interface to implement if you want to dynamically
* mark methods as constructor. As opposed to simply list them in the configuration file.
*
* To register it in the configuration file use the `phpstan.additionalConstructorsExtension` service tag:
*
* ```
* services:
* -
* class: App\PHPStan\MyExtension
* tags:
* - phpstan.additionalConstructorsExtension
* ```
*
* @api
*/
interface AdditionalConstructorsExtension
{

public const EXTENSION_TAG = 'phpstan.additionalConstructorsExtension';

/** @return string[] */
public function getAdditionalConstructors(ClassReflection $classReflection): array;

}
11 changes: 11 additions & 0 deletions src/Reflection/ConstructorsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Reflection;

use PHPStan\DependencyInjection\Container;
use ReflectionException;
use function array_key_exists;
use function explode;
Expand All @@ -16,6 +17,7 @@ final class ConstructorsHelper
* @param list<string> $additionalConstructors
*/
public function __construct(
private Container $container,
private array $additionalConstructors,
)
{
Expand All @@ -34,6 +36,15 @@ public function getConstructors(ClassReflection $classReflection): array
$constructors[] = $classReflection->getConstructor()->getName();
}

/** @var AdditionalConstructorsExtension[] $extensions */
$extensions = $this->container->getServicesByTag(AdditionalConstructorsExtension::EXTENSION_TAG);
foreach ($extensions as $extension) {
$extensionConstructors = $extension->getAdditionalConstructors($classReflection);
foreach ($extensionConstructors as $extensionConstructor) {
$constructors[] = $extensionConstructor;
}
}

$nativeReflection = $classReflection->getNativeReflection();
foreach ($this->additionalConstructors as $additionalConstructor) {
[$className, $methodName] = explode('::', $additionalConstructor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ class MissingReadOnlyByPhpDocPropertyAssignRuleTest extends RuleTestCase
protected function getRule(): Rule
{
return new MissingReadOnlyByPhpDocPropertyAssignRule(
new ConstructorsHelper([
'MissingReadOnlyPropertyAssignPhpDoc\\TestCase::setUp',
]),
new ConstructorsHelper(
self::getContainer(),
[
'MissingReadOnlyPropertyAssignPhpDoc\\TestCase::setUp',
],
),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ class MissingReadOnlyPropertyAssignRuleTest extends RuleTestCase
protected function getRule(): Rule
{
return new MissingReadOnlyPropertyAssignRule(
new ConstructorsHelper([
'MissingReadOnlyPropertyAssign\\TestCase::setUp',
]),
new ConstructorsHelper(
self::getContainer(),
[
'MissingReadOnlyPropertyAssign\\TestCase::setUp',
],
),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected function getRule(): Rule
return new ReadOnlyByPhpDocPropertyAssignRule(
new PropertyReflectionFinder(),
new ConstructorsHelper(
self::getContainer(),
[
'ReadonlyPropertyAssignPhpDoc\\TestCase::setUp',
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected function getRule(): Rule
return new ReadOnlyPropertyAssignRule(
new PropertyReflectionFinder(),
new ConstructorsHelper(
self::getContainer(),
[
'ReadonlyPropertyAssign\\TestCase::setUp',
],
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Properties/UninitializedPropertyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ protected function getRule(): Rule
{
return new UninitializedPropertyRule(
new ConstructorsHelper(
self::getContainer(),
[
'UninitializedProperty\\TestCase::setUp',
],
Expand Down Expand Up @@ -48,6 +49,13 @@ public function isInitialized(PropertyReflection $property, string $propertyName
];
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/uninitialized-property-rule.neon',
];
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/uninitialized-property.php'], [
Expand Down Expand Up @@ -113,4 +121,18 @@ public function testBug7219(): void
]);
}

public function testAdditionalConstructorsExtension(): void
{
$this->analyse([__DIR__ . '/data/uninitialized-property-additional-constructors.php'], [
[
'Class TestInitializedProperty\TestAdditionalConstructor has an uninitialized property $one. Give it default value or assign it in the constructor.',
07,
],
[
'Class TestInitializedProperty\TestAdditionalConstructor has an uninitialized property $three. Give it default value or assign it in the constructor.',
11,
],
]);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Properties;

use PHPStan\Reflection\AdditionalConstructorsExtension;
use PHPStan\Reflection\ClassReflection;

class TestInitializedProperty implements AdditionalConstructorsExtension
{

public function getAdditionalConstructors(ClassReflection $classReflection): array
{
if ($classReflection->getName() === 'TestInitializedProperty\\TestAdditionalConstructor') {
return ['setTwo'];
}

return [];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php // lint >= 7.4

namespace TestInitializedProperty;

class TestAdditionalConstructor
{
public string $one;

protected int $two;

protected int $three;

public function setTwo(int $value): void
{
$this->two = $value;
}

public function setThree(int $value): void
{
$this->three = $value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
-
class: PHPStan\Rules\Properties\TestInitializedProperty
tags:
- phpstan.additionalConstructorsExtension