-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- ExtendedProduct wasn't resolved and needed extra TLC TODO: Tests for mixed occurrences of Interface and Attributes Add attributes from spike - yet without tests
- Loading branch information
Showing
18 changed files
with
535 additions
and
14 deletions.
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ebln\PHPStan\EnforceFactory; | ||
|
||
/** | ||
* Marks classes to be instanciated by certain factories | ||
* | ||
* If used together with ForceFactoryInterface | ||
* the configured factories must be congruent! | ||
* This is enforced for PHP 8 and later. | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
class ForceFactory | ||
{ | ||
/** @var array<int, class-string> */ | ||
private array $allowedFactories; | ||
|
||
/** @param class-string ...$factories */ | ||
public function __construct(string ...$factories) | ||
{ | ||
$allowedFactories = []; | ||
foreach ($factories as $factory) { | ||
$allowedFactories[$factory] = $factory; | ||
} | ||
|
||
$this->allowedFactories = array_values($allowedFactories); | ||
} | ||
|
||
/** @return array<int, class-string> */ | ||
public function getAllowedFactories(): array | ||
{ | ||
return $this->allowedFactories; | ||
} | ||
} |
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,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test\Ebln\PHPStan\EnforceFactory; | ||
|
||
use Ebln\PHPStan\EnforceFactory\ForceFactoryRule; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @requires PHP >= 8.0 | ||
* @extends RuleTestCase<ForceFactoryRule> | ||
*/ | ||
class AttribForceFactoryRuleTest extends RuleTestCase | ||
{ | ||
private const ERROR_MESSAGE = 'Test\Ebln\PHPStan\EnforceFactory\dataAttrib\code\ForcedFactoryProduct must be instantiated by Test\Ebln\PHPStan\EnforceFactory\dataAttrib\ForcedFactory or Test\Ebln\PHPStan\EnforceFactory\dataAttrib\TraitFactory!'; | ||
|
||
// Sadly this remains a vector, as phpstan fails to infer the created class name | ||
public function testLoopholeFactory(): void | ||
{ | ||
$this->analyse([__DIR__ . '/dataAttrib/LoopholeFactory.php'], []); | ||
} | ||
|
||
public function testEmptyAllowedClasses(): void | ||
{ | ||
$this->analyse([__DIR__ . '/dataAttrib/EmptyFactory.php'], [ | ||
[ | ||
'Test\Ebln\PHPStan\EnforceFactory\dataAttrib\code\EmptyProduct has either no factories defined or a conflict between interface and attribute!', | ||
13, | ||
], | ||
]); | ||
} | ||
|
||
public function testRogueFactory(): void | ||
{ | ||
$this->analyse([__DIR__ . '/dataAttrib/RogueFactory.php'], [ | ||
[self::ERROR_MESSAGE, 15], | ||
[self::ERROR_MESSAGE, 22], | ||
[self::ERROR_MESSAGE, 29], | ||
[self::ERROR_MESSAGE, 40], | ||
[self::ERROR_MESSAGE, 40], | ||
[self::ERROR_MESSAGE, 51], | ||
[self::ERROR_MESSAGE, 56], | ||
['Test\Ebln\PHPStan\EnforceFactory\dataAttrib\code\ExtendedProduct must be instantiated by Test\Ebln\PHPStan\EnforceFactory\dataAttrib\ForcedFactory or Test\Ebln\PHPStan\EnforceFactory\dataAttrib\TraitFactory!', 69], | ||
[self::ERROR_MESSAGE, 95], | ||
]); | ||
} | ||
|
||
public function testRogueFactoryAndTrait(): void | ||
{ | ||
$this->analyse([__DIR__ . '/dataAttrib/RogueTraitFactory.php', __DIR__ . '/dataAttrib/FactoryTrait.php'], [ | ||
[self::ERROR_MESSAGE, 13], | ||
[self::ERROR_MESSAGE, 20], | ||
[self::ERROR_MESSAGE, 27], | ||
]); | ||
} | ||
|
||
public function testTraitedFactory(): void | ||
{ | ||
$this->analyse([__DIR__ . '/dataAttrib/TraitFactory.php', __DIR__ . '/dataAttrib/FactoryTrait.php'], []); | ||
} | ||
|
||
public function testAllowedFactory(): void | ||
{ | ||
$this->analyse([__DIR__ . '/dataAttrib/ForcedFactory.php'], []); | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new ForceFactoryRule($this->createReflectionProvider()); | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test\Ebln\PHPStan\EnforceFactory\dataAttrib; | ||
|
||
use Test\Ebln\PHPStan\EnforceFactory\dataAttrib\code\EmptyProduct; | ||
|
||
class EmptyFactory | ||
{ | ||
public function class(): EmptyProduct | ||
{ | ||
return new EmptyProduct(); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test\Ebln\PHPStan\EnforceFactory\dataAttrib; | ||
|
||
use Test\Ebln\PHPStan\EnforceFactory\dataAttrib\code\ForcedFactoryProduct; | ||
|
||
trait FactoryTrait | ||
{ | ||
public function traitedClass(): ForcedFactoryProduct | ||
{ | ||
return new ForcedFactoryProduct(); | ||
} | ||
|
||
public function traitedClassVariable(): void | ||
{ | ||
$class = ForcedFactoryProduct::class; | ||
|
||
$new = new $class(); | ||
} | ||
|
||
public function traitedStringVariable(): void | ||
{ | ||
$class = '\Test\Ebln\PHPStan\EnforceFactory\dataAttrib\code\ForcedFactoryProduct'; | ||
|
||
$new = new $class(); | ||
} | ||
} |
Oops, something went wrong.