Skip to content

Commit

Permalink
[BUILD] Added more strict phpstan rules (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Jul 31, 2019
2 parents 34546e2 + f1867fd commit 94b4582
Show file tree
Hide file tree
Showing 20 changed files with 82 additions and 180 deletions.
12 changes: 9 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
includes:
- vendor-bin/phpstan/vendor/jangregor/phpstan-prophecy/src/extension.neon

parameters:
autoload_files:
- vendor-bin/test/vendor/autoload.php
Expand All @@ -11,5 +8,14 @@ parameters:

# Symfony DI
- '#Cannot call method end\(\) on Symfony\\Component\\Config\\Definition\\Builder\\NodeParentInterface\|null.#'
- "/Call to function method_exists.. with 'Symfony.+' and 'getRootNode' will always evaluate to false./"

# PHPUnit
-
message: '#Property .*::\$.* has no typehint specified.#'
path: tests/
-
message: '#Method Core23\\Doctrine\\Tests\\Model\\Traits\\.*::createTraitMock\(\) has no return typehint specified.#'
path: tests/Model/Traits/

- '#Core23\\Doctrine\\Tests\\Fixtures\\DemoEntityManager::__construct\(\) does not call parent constructor from Sonata\\Doctrine\\Model\\BaseManager.#'
2 changes: 1 addition & 1 deletion src/Bridge/Symfony/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder('core23_doctrine');

// Keep compatibility with symfony/config < 4.2
if (!method_exists($treeBuilder, 'getRootNode')) {
if (!method_exists(TreeBuilder::class, 'getRootNode')) {
$rootNode = $treeBuilder->root('core23_doctrine');
} else {
$rootNode = $treeBuilder->getRootNode();
Expand Down
6 changes: 0 additions & 6 deletions src/EventListener/ORM/ConfirmableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
use Core23\Doctrine\Util\ClassUtils;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use LogicException;

final class ConfirmableListener extends AbstractListener
{
Expand All @@ -35,10 +33,6 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
{
$meta = $eventArgs->getClassMetadata();

if (!$meta instanceof ClassMetadata) {
throw new LogicException('Class metadata was no ORM');
}

$reflClass = $meta->getReflectionClass();

if (null === $reflClass || !ClassUtils::containsTrait($reflClass, ConfirmableTrait::class)) {
Expand Down
6 changes: 0 additions & 6 deletions src/EventListener/ORM/DeletableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
use Core23\Doctrine\Util\ClassUtils;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use LogicException;

final class DeletableListener extends AbstractListener
{
Expand All @@ -35,10 +33,6 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
{
$meta = $eventArgs->getClassMetadata();

if (!$meta instanceof ClassMetadata) {
throw new LogicException('Class metadata was no ORM');
}

$reflClass = $meta->getReflectionClass();

if (null === $reflClass || !ClassUtils::containsTrait($reflClass, DeleteableTrait::class)) {
Expand Down
6 changes: 0 additions & 6 deletions src/EventListener/ORM/LifecycleDateListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use LogicException;

final class LifecycleDateListener extends AbstractListener
{
Expand Down Expand Up @@ -65,10 +63,6 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
{
$meta = $eventArgs->getClassMetadata();

if (!$meta instanceof ClassMetadata) {
throw new LogicException('Class metadata was no ORM');
}

$reflClass = $meta->getReflectionClass();

if (null === $reflClass || !ClassUtils::containsTrait($reflClass, LifecycleDateTimeTrait::class)) {
Expand Down
15 changes: 6 additions & 9 deletions src/EventListener/ORM/SortableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\UnitOfWork;
use LogicException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

Expand Down Expand Up @@ -98,10 +96,6 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
{
$meta = $eventArgs->getClassMetadata();

if (!$meta instanceof ClassMetadata) {
throw new LogicException('Class metadata was no ORM');
}

$reflClass = $meta->getReflectionClass();

if (null === $reflClass || !ClassUtils::containsTrait($reflClass, SortableTrait::class)) {
Expand All @@ -124,7 +118,7 @@ private function uniquePosition(LifecycleEventArgs $args, ?int $oldPosition = nu
if (null === $entity->getPosition()) {
$position = $this->getNextPosition($args->getEntityManager(), $entity);
$entity->setPosition($position);
} elseif ($oldPosition && $oldPosition !== $entity->getPosition()) {
} elseif (null !== $oldPosition && $oldPosition !== $entity->getPosition()) {
$this->movePosition($args->getEntityManager(), $entity);
}
}
Expand Down Expand Up @@ -169,10 +163,13 @@ private function getNextPosition(EntityManager $em, PositionAwareInterface $enti
try {
$result = $qb->getQuery()->getOneOrNullResult();

return ($result instanceof PositionAwareInterface ? $result->getPosition() : 0) + 1;
if ($result instanceof PositionAwareInterface && null !== $result->getPosition()) {
return $result->getPosition() + 1;
}
} catch (NonUniqueResultException $ignored) {
return 0;
}

return 0;
}

/**
Expand Down
6 changes: 0 additions & 6 deletions src/EventListener/ORM/UniqueActiveListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\UnitOfWork;
use LogicException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

Expand Down Expand Up @@ -69,10 +67,6 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
{
$meta = $eventArgs->getClassMetadata();

if (!$meta instanceof ClassMetadata) {
throw new LogicException('Class metadata was no ORM');
}

$reflClass = $meta->getReflectionClass();

if (null === $reflClass || !$reflClass->implementsInterface(UniqueActiveInterface::class)) {
Expand Down
7 changes: 5 additions & 2 deletions src/Test/ORM/EntityManagerMockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class EntityManagerMockFactory
*
* @return EntityManager|MockObject
*/
public static function create(TestCase $test, Closure $qbCallback, $fields): MockObject
public static function create(TestCase $test, Closure $qbCallback, array $fields): MockObject
{
$qb = $test->getMockBuilder(QueryBuilder::class)->disableOriginalConstructor()->getMock();

Expand Down Expand Up @@ -65,7 +65,10 @@ private static function prepareQueryBuilder(TestCase $test, MockObject $qb): voi
$qb->method('leftJoin')->willReturn($qb);
}

private static function prepareMetadata(TestCase $test, $fields): MockObject
/**
* @param string[] $fields
*/
private static function prepareMetadata(TestCase $test, array $fields): MockObject
{
$metadata = $test->getMockBuilder(ClassMetadataInfo::class)->disableOriginalConstructor()->getMock();
$metadata->method('getFieldNames')->willReturn($fields);
Expand Down
8 changes: 0 additions & 8 deletions tests/Bridge/Symfony/Bundle/Core23DoctrineBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,9 @@
use Core23\Doctrine\Bridge\Symfony\Bundle\Core23DoctrineBundle;
use Core23\Doctrine\Bridge\Symfony\DependencyInjection\Core23DoctrineExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;

final class Core23DoctrineBundleTest extends TestCase
{
public function testItIsInstantiable(): void
{
$bundle = new Core23DoctrineBundle();

static::assertInstanceOf(BundleInterface::class, $bundle);
}

public function testGetPath(): void
{
$bundle = new Core23DoctrineBundle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
namespace Core23\Doctrine\Tests\Bridge\Symfony\DependencyInjection;

use Core23\Doctrine\Bridge\Symfony\DependencyInjection\Core23DoctrineExtension;
use Core23\Doctrine\EventListener\ORM\ConfirmableListener;
use Core23\Doctrine\EventListener\ORM\DeletableListener;
use Core23\Doctrine\EventListener\ORM\LifecycleDateListener;
use Core23\Doctrine\EventListener\ORM\SortableListener;
use Core23\Doctrine\EventListener\ORM\TablePrefixEventListener;
use Core23\Doctrine\EventListener\ORM\UniqueActiveListener;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;

final class Core23DoctrineExtensionTest extends AbstractExtensionTestCase
Expand All @@ -20,7 +26,12 @@ public function testLoadDefault(): void
{
$this->load();

static::assertTrue(true);
$this->assertContainerBuilderHasService(ConfirmableListener::class);
$this->assertContainerBuilderHasService(DeletableListener::class);
$this->assertContainerBuilderHasService(LifecycleDateListener::class);
$this->assertContainerBuilderHasService(SortableListener::class);
$this->assertContainerBuilderHasService(UniqueActiveListener::class);
$this->assertContainerBuilderHasService(TablePrefixEventListener::class);
}

protected function getContainerExtensions()
Expand Down
23 changes: 0 additions & 23 deletions tests/EventListener/ORM/ConfirmableListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,15 @@
use Core23\Doctrine\EventListener\ORM\ConfirmableListener;
use Core23\Doctrine\Tests\Fixtures\ClassWithAllProperties;
use Core23\Doctrine\Tests\Fixtures\EmptyClass;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use LogicException;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use ReflectionClass;

final class ConfirmableListenerTest extends TestCase
{
public function testItIsInstantiable(): void
{
$listener = new ConfirmableListener();

static::assertInstanceOf(EventSubscriber::class, $listener);
}

public function testGetSubscribedEvents(): void
{
$listener = new ConfirmableListener();
Expand All @@ -39,20 +30,6 @@ public function testGetSubscribedEvents(): void
], $listener->getSubscribedEvents());
}

public function testLoadClassMetadataWithNoValidData(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Class metadata was no ORM');

$eventArgs = $this->prophesize(LoadClassMetadataEventArgs::class);
$eventArgs->getClassMetadata()
->willReturn(null)
;

$listener = new ConfirmableListener();
$listener->loadClassMetadata($eventArgs->reveal());
}

public function testLoadClassMetadataWithEmptyClass(): void
{
$metadata = $this->prophesize(ClassMetadata::class);
Expand Down
23 changes: 0 additions & 23 deletions tests/EventListener/ORM/DeletableListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,15 @@
use Core23\Doctrine\EventListener\ORM\DeletableListener;
use Core23\Doctrine\Tests\Fixtures\ClassWithAllProperties;
use Core23\Doctrine\Tests\Fixtures\EmptyClass;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use LogicException;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use ReflectionClass;

final class DeletableListenerTest extends TestCase
{
public function testItIsInstantiable(): void
{
$listener = new DeletableListener();

static::assertInstanceOf(EventSubscriber::class, $listener);
}

public function testGetSubscribedEvents(): void
{
$listener = new DeletableListener();
Expand All @@ -39,20 +30,6 @@ public function testGetSubscribedEvents(): void
], $listener->getSubscribedEvents());
}

public function testLoadClassMetadataWithNoValidData(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Class metadata was no ORM');

$eventArgs = $this->prophesize(LoadClassMetadataEventArgs::class);
$eventArgs->getClassMetadata()
->willReturn(null)
;

$listener = new DeletableListener();
$listener->loadClassMetadata($eventArgs->reveal());
}

public function testLoadClassMetadataWithEmptyClass(): void
{
$metadata = $this->prophesize(ClassMetadata::class);
Expand Down
Loading

0 comments on commit 94b4582

Please sign in to comment.