Skip to content

Commit

Permalink
SA: Bump Psalm to level 5
Browse files Browse the repository at this point in the history
  • Loading branch information
ostrolucky committed Apr 5, 2021
1 parent e3d6b03 commit 574e7ad
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 8 deletions.
3 changes: 3 additions & 0 deletions DataCollector/DoctrineDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Tools\SchemaValidator;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
use Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector as BaseCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -102,6 +103,8 @@ public function collect(Request $request, Response $response, ?Throwable $except
$factory = $em->getMetadataFactory();
$validator = new SchemaValidator($em);

assert($factory instanceof AbstractClassMetadataFactory);

foreach ($factory->getLoadedMetadata() as $class) {
assert($class instanceof ClassMetadataInfo);
if (isset($entities[$name][$class->getName()])) {
Expand Down
2 changes: 2 additions & 0 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,8 @@ private function loadValidatorLoader(string $entityManagerName, ContainerBuilder
/**
* @param array<string, mixed> $objectManager
* @param string $cacheName
*
* @psalm-suppress MoreSpecificImplementedParamType
*/
public function loadObjectManagerCacheDriver(array $objectManager, ContainerBuilder $container, $cacheName)
{
Expand Down
8 changes: 6 additions & 2 deletions DoctrineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterUidTypePass;
use Symfony\Bridge\Doctrine\DependencyInjection\Security\UserProvider\EntityFactory;
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
use Symfony\Component\Console\Application;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -40,8 +41,11 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine.connections', 'doctrine.dbal.%s_connection.event_manager', 'doctrine'), PassConfig::TYPE_BEFORE_OPTIMIZATION);

if ($container->hasExtension('security')) {
/** @psalm-suppress MissingDependency We can safely assume here that if security extension exists, all security deps are installed */
$container->getExtension('security')->addUserProviderFactory(new EntityFactory('entity', 'doctrine.orm.security.user.provider'));
$security = $container->getExtension('security');

if ($security instanceof SecurityExtension) {
$security->addUserProviderFactory(new EntityFactory('entity', 'doctrine.orm.security.user.provider'));
}
}

$container->addCompilerPass(new DoctrineValidationPass('orm'));
Expand Down
3 changes: 2 additions & 1 deletion Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataFactory as BaseClassMetadataFactory;
use Doctrine\ORM\Mapping\ClassMetadataInfo;

class ClassMetadataFactory extends BaseClassMetadataFactory
{
Expand All @@ -16,7 +17,7 @@ protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonS

$customGeneratorDefinition = $class->customGeneratorDefinition;

if (! isset($customGeneratorDefinition['instance'])) {
if (! isset($customGeneratorDefinition['instance']) || ! $class instanceof ClassMetadataInfo) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions Mapping/MappingDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata): void
$metadata->generatorType !== ClassMetadataInfo::GENERATOR_TYPE_CUSTOM
|| ! isset($metadata->customGeneratorDefinition['class'])
|| ! $this->idGeneratorLocator->has($metadata->customGeneratorDefinition['class'])
|| ! $metadata instanceof ClassMetadataInfo
) {
return;
}
Expand Down
8 changes: 7 additions & 1 deletion Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ public function __construct(ContainerInterface $container, array $connections, a
public function getAliasNamespace($alias)
{
foreach (array_keys($this->getManagers()) as $name) {
$objectManager = $this->getManager($name);

if (! $objectManager instanceof EntityManagerInterface) {
continue;
}

try {
return $this->getManager($name)->getConfiguration()->getEntityNamespace($alias);
return $objectManager->getConfiguration()->getEntityNamespace($alias);
} catch (ORMException $e) {
}
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/Command/CreateDatabaseDoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public function provideShardOption(): Generator
/**
* @param mixed[]|null $params Connection parameters
*
* @return MockObject&Container
*
* @psalm-param Params $params
*/
private function getMockContainer(string $connectionName, ?array $params = null): MockObject
Expand Down
11 changes: 8 additions & 3 deletions Tests/Command/DropDatabaseDoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Doctrine\Bundle\DoctrineBundle\Tests\Command;

use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception;
use Doctrine\Persistence\ManagerRegistry;
use Generator;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\Container;

use function array_merge;
use function class_exists;
Expand Down Expand Up @@ -140,20 +143,22 @@ public function provideIncompatibleDriverOptions(): Generator
/**
* @param list<mixed> $params Connection parameters
*
* @return MockObject&Container
*
* @psalm-param Params $params
*/
private function getMockContainer(string $connectionName, array $params): MockObject
{
// Mock the container and everything you'll need here
$mockDoctrine = $this->getMockBuilder('Doctrine\Persistence\ManagerRegistry')
$mockDoctrine = $this->getMockBuilder(ManagerRegistry::class)
->getMock();

$mockDoctrine->expects($this->any())
->method('getDefaultConnectionName')
->withAnyParameters()
->willReturn($connectionName);

$mockConnection = $this->getMockBuilder('Doctrine\DBAL\Connection')
$mockConnection = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->setMethods(['getParams'])
->getMockForAbstractClass();
Expand All @@ -168,7 +173,7 @@ private function getMockContainer(string $connectionName, array $params): MockOb
->withAnyParameters()
->willReturn($mockConnection);

$mockContainer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Container')
$mockContainer = $this->getMockBuilder(Container::class)
->setMethods(['get'])
->getMock();

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"symfony/phpunit-bridge": "^5.2",
"symfony/property-info": "^4.3.3|^5.0",
"symfony/proxy-manager-bridge": "^3.4|^4.3.3|^5.0",
"symfony/security-bundle": "^4.4|5.0",
"symfony/twig-bridge": "^3.4.30|^4.3.3|^5.0",
"symfony/validator": "^3.4.30|^4.3.3|^5.0",
"symfony/web-profiler-bundle": "^3.4.30|^4.3.3|^5.0",
Expand Down
2 changes: 1 addition & 1 deletion psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="6"
errorLevel="5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down

0 comments on commit 574e7ad

Please sign in to comment.