Check Doctrine entities by PHPStan
[MAILPOET-2945]
This commit is contained in:
36
tasks/phpstan/create-entity-manager.php
Normal file
36
tasks/phpstan/create-entity-manager.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
//require_once __DIR__ . '/../../vendor/autoload.php';
|
||||
|
||||
use MailPoet\Doctrine\Annotations\AnnotationReaderProvider;
|
||||
use MailPoet\Doctrine\ConfigurationFactory;
|
||||
use MailPoet\Doctrine\ConnectionFactory;
|
||||
use MailPoetVendor\Doctrine\ORM\EntityManager;
|
||||
use MailPoetVendor\Doctrine\ORM\Mapping\Driver\AnnotationDriver;
|
||||
|
||||
$annotationReaderProvider = new AnnotationReaderProvider();
|
||||
$annotationReader = $annotationReaderProvider->getAnnotationReader();
|
||||
$configuration = (new ConfigurationFactory(false, $annotationReaderProvider))->createConfiguration();
|
||||
$configuration->setMetadataDriverImpl(
|
||||
new class($annotationReader, [ConfigurationFactory::ENTITY_DIR]) extends AnnotationDriver {
|
||||
// Returning 'isTransient' = true means 'do not try to load Doctrine metadata' (which is true for most classes).
|
||||
// Here we override the method to check agains the ENTITY_DIR since phpstan-doctrine seems to sometimes pass
|
||||
// non-entity classes with non-Doctrine annotations to this methods. It may be fixed in future versions.
|
||||
public function isTransient($className) {
|
||||
$reflection = new ReflectionClass($className);
|
||||
if ($reflection && $reflection->getFileName()) {
|
||||
$entityDirRealpath = realpath(ConfigurationFactory::ENTITY_DIR);
|
||||
$fileRealpath = realpath($reflection->getFileName());
|
||||
if (substr($fileRealpath, 0, strlen($entityDirRealpath)) !== $entityDirRealpath) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return parent::isTransient($className);
|
||||
}
|
||||
}
|
||||
);
|
||||
$platformClass = ConnectionFactory::PLATFORM_CLASS;
|
||||
return EntityManager::create([
|
||||
'driver' => ConnectionFactory::DRIVER,
|
||||
'platform' => new $platformClass,
|
||||
], $configuration);
|
@@ -24,6 +24,8 @@ parameters:
|
||||
dynamicConstantNames:
|
||||
- WP_DEBUG
|
||||
- MAILPOET_PREMIUM_INITIALIZED
|
||||
doctrine:
|
||||
objectManagerLoader: create-entity-manager.php
|
||||
|
||||
# exclude level 6 errors
|
||||
checkGenericClassInNonGenericObjectType: false
|
||||
@@ -32,4 +34,5 @@ parameters:
|
||||
checkMissingTypehints: false
|
||||
includes:
|
||||
- vendor/phpstan/phpstan-doctrine/extension.neon
|
||||
- vendor/phpstan/phpstan-doctrine/rules.neon
|
||||
- phpstan-baseline.neon # https://medium.com/@ondrejmirtes/phpstans-baseline-feature-lets-you-hold-new-code-to-a-higher-standard-e77d815a5dff
|
||||
|
Reference in New Issue
Block a user