-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/1.3.x' into 1.4.x
- Loading branch information
Showing
8 changed files
with
170 additions
and
0 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,37 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Classes; | ||
|
||
use Doctrine\Persistence\Proxy; | ||
use PHPStan\Type\Doctrine\ObjectMetadataResolver; | ||
|
||
class DoctrineProxyForbiddenClassNamesExtension implements ForbiddenClassNameExtension | ||
{ | ||
|
||
/** @var ObjectMetadataResolver */ | ||
private $objectMetadataResolver; | ||
|
||
public function __construct(ObjectMetadataResolver $objectMetadataResolver) | ||
{ | ||
$this->objectMetadataResolver = $objectMetadataResolver; | ||
} | ||
|
||
public function getClassPrefixes(): array | ||
{ | ||
$objectManager = $this->objectMetadataResolver->getObjectManager(); | ||
if ($objectManager === null) { | ||
return []; | ||
} | ||
|
||
$entityManagerInterface = 'Doctrine\ORM\EntityManagerInterface'; | ||
|
||
if (!$objectManager instanceof $entityManagerInterface) { | ||
return []; | ||
} | ||
|
||
return [ | ||
'Doctrine' => $objectManager->getConfiguration()->getProxyNamespace() . '\\' . Proxy::MARKER, | ||
]; | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
tests/Classes/DoctrineProxyForbiddenClassNamesExtensionTest.php
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,47 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Classes; | ||
|
||
use PHPStan\Rules\Classes\InstantiationRule; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use function array_merge; | ||
|
||
/** | ||
* @extends RuleTestCase<InstantiationRule> | ||
*/ | ||
class DoctrineProxyForbiddenClassNamesExtensionTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return self::getContainer()->getByType(InstantiationRule::class); | ||
} | ||
|
||
public function testForbiddenClassNameExtension(): void | ||
{ | ||
$this->analyse( | ||
[__DIR__ . '/data/forbidden-class-name.php'], | ||
[ | ||
[ | ||
'Referencing prefixed Doctrine class: App\GeneratedProxy\__CG__\App\TestDoctrineEntity.', | ||
19, | ||
'This is most likely unintentional. Did you mean to type \App\TestDoctrineEntity?', | ||
], | ||
[ | ||
'Referencing prefixed PHPStan class: _PHPStan_15755dag8c\TestPhpStanEntity.', | ||
20, | ||
'This is most likely unintentional. Did you mean to type \TestPhpStanEntity?', | ||
], | ||
] | ||
); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return array_merge(parent::getAdditionalConfigFiles(), [ | ||
__DIR__ . '/phpstan.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,20 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace App\GeneratedProxy\__CG__\App; | ||
|
||
class TestDoctrineEntity | ||
{ | ||
} | ||
|
||
namespace _PHPStan_15755dag8c; | ||
|
||
class TestPhpStanEntity | ||
{ | ||
} | ||
|
||
namespace ForbiddenNameClassExtension; | ||
|
||
use App\GeneratedProxy\__CG__\App\TestEntity; | ||
|
||
$doctrineEntity = new \App\GeneratedProxy\__CG__\App\TestDoctrineEntity(); | ||
$phpStanEntity = new \_PHPStan_15755dag8c\TestPhpStanEntity(); |
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,45 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
use Cache\Adapter\PHPArray\ArrayCachePool; | ||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use Doctrine\DBAL\DriverManager; | ||
use Doctrine\DBAL\Types\DateTimeImmutableType; | ||
use Doctrine\DBAL\Types\Type; | ||
use Doctrine\ORM\Configuration; | ||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; | ||
use Doctrine\ORM\Mapping\Driver\AttributeDriver; | ||
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; | ||
|
||
$config = new Configuration(); | ||
$config->setProxyDir(__DIR__); | ||
$config->setProxyNamespace('App\GeneratedProxy'); | ||
$config->setMetadataCache(new ArrayCachePool()); | ||
|
||
$metadataDriver = new MappingDriverChain(); | ||
$metadataDriver->addDriver(new AnnotationDriver( | ||
new AnnotationReader(), | ||
[__DIR__ . '/data'] | ||
), 'PHPStan\\Rules\\Doctrine\\ORM\\'); | ||
|
||
if (PHP_VERSION_ID >= 80100) { | ||
$metadataDriver->addDriver( | ||
new AttributeDriver([__DIR__ . '/data-attributes']), | ||
'PHPStan\\Rules\\Doctrine\\ORMAttributes\\' | ||
); | ||
} | ||
|
||
$config->setMetadataDriverImpl($metadataDriver); | ||
|
||
Type::overrideType( | ||
'date', | ||
DateTimeImmutableType::class | ||
); | ||
|
||
return new EntityManager( | ||
DriverManager::getConnection([ | ||
'driver' => 'pdo_sqlite', | ||
'memory' => true, | ||
]), | ||
$config | ||
); |
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 @@ | ||
includes: | ||
- ../../extension.neon | ||
|
||
parameters: | ||
doctrine: | ||
objectManagerLoader: entity-manager.php |