-
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
- Loading branch information
Showing
16 changed files
with
440 additions
and
8 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
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
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(); | ||
} | ||
} |
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,109 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test\Ebln\PHPStan\EnforceFactory\dataAttrib; | ||
|
||
use Test\Ebln\PHPStan\EnforceFactory\dataAttrib\code\ExtendedProduct; | ||
use Test\Ebln\PHPStan\EnforceFactory\dataAttrib\code\ForcedFactoryProduct; | ||
use Test\Ebln\PHPStan\EnforceFactory\dataAttrib\code\FreeProduct; | ||
|
||
class ForcedFactory | ||
{ | ||
public function class(): ForcedFactoryProduct | ||
{ | ||
return new ForcedFactoryProduct(); | ||
} | ||
|
||
public function classVariable(): void | ||
{ | ||
$class = ForcedFactoryProduct::class; | ||
|
||
$new = new $class(); | ||
} | ||
|
||
public function stringVariable(): void | ||
{ | ||
$class = '\Test\Ebln\PHPStan\EnforceFactory\dataAttrib\code\ForcedFactoryProduct'; | ||
|
||
$new = new $class(); | ||
} | ||
|
||
public function variableMixed(bool $toggle): void | ||
{ | ||
if ($toggle) { | ||
$class = '\Test\Ebln\PHPStan\EnforceFactory\dataAttrib\code\ForcedFactoryProduct'; | ||
} else { | ||
$class = ForcedFactoryProduct::class; | ||
} | ||
|
||
$new = new $class(); | ||
} | ||
|
||
public function variableMixedProducts(bool $toggle): void | ||
{ | ||
if ($toggle) { | ||
$class = '\Test\Ebln\PHPStan\EnforceFactory\dataAttrib\code\ForcedFactoryProduct'; | ||
} else { | ||
$class = FreeProduct::class; | ||
} | ||
|
||
$new = new $class(); | ||
} | ||
|
||
// TODO needs to fail! | ||
public function variableUnpredictable(bool $toggle): void | ||
{ | ||
if ($toggle) { | ||
$class = '\Test\Ebln\PHPStan\EnforceFactory\dataAttrib\code\ForcedFactoryProduct'; | ||
} else { | ||
$class = 'Hello world-' . random_int(10, 99); | ||
} | ||
|
||
$new = new $class(); | ||
} | ||
|
||
public function anonymousExtending(): void | ||
{ | ||
$x = new class() extends ForcedFactoryProduct | ||
{ | ||
public function foo(): string | ||
{ | ||
return 'bar'; | ||
} | ||
}; | ||
|
||
$bar = $x->foo(); | ||
} | ||
|
||
public function anonymousExtendingSquare(): void | ||
{ | ||
$x = new class() extends ExtendedProduct | ||
{ | ||
public function foo(): string | ||
{ | ||
return 'bar'; | ||
} | ||
}; | ||
|
||
$bar = $x->foo(); | ||
} | ||
|
||
public function anonymousPassing(): void | ||
{ | ||
$x = new class() | ||
{ | ||
public function foo(): string | ||
{ | ||
return 'bar'; | ||
} | ||
}; | ||
|
||
$bar = $x->foo(); | ||
} | ||
|
||
public static function staticClass(): ForcedFactoryProduct | ||
{ | ||
return new ForcedFactoryProduct(); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test\Ebln\PHPStan\EnforceFactory\dataAttrib; | ||
|
||
class LoopholeFactory | ||
{ | ||
public function variableUninferable(bool $toggle): void | ||
{ | ||
if ($toggle) { | ||
$class = '\Test\Ebln\PHPStan\EnforceFactory\dataAttrib\code\ForcedFactoryProduct'; | ||
} else { | ||
$class = 'Hello world-' . random_int(10, 99); | ||
} | ||
|
||
$new = new $class(); | ||
} | ||
} |
Oops, something went wrong.