Skip to content

Commit

Permalink
setup phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Apr 6, 2024
1 parent 389449f commit 9341e85
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 223 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
"doctrine/phpcr-bundle": "^3.0",
"doctrine/phpcr-odm": "^2.0",
"jackalope/jackalope-doctrine-dbal": "^2.0",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-doctrine": "^1.3",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-symfony": "^1.3",
"phpunit/phpunit": "^9.5",
"matthiasnoback/symfony-dependency-injection-test": "^4.1.0 || ^5.1.0",
"matthiasnoback/symfony-config-test": "^4.1.0 || ^5.1.0",
"symfony/phpunit-bridge": "^7.0.3",
Expand Down
15 changes: 15 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
includes:
- vendor/phpstan/phpstan-doctrine/extension.neon
- vendor/phpstan/phpstan-doctrine/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-symfony/rules.neon
parameters:
level: 2
paths:
- src/
- tests/
excludePaths:
- tests/Fixtures/App/var/
- tests/Fixtures/App/config/
- tests/Fixtures/fixtures/config/
4 changes: 2 additions & 2 deletions src/Doctrine/Orm/RouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public function getRouteCollectionForRequest(Request $request): RouteCollection
if (0 === \count($candidates)) {
return $collection;
}
$routes = $this->getRouteRepository()->findByStaticPrefix($candidates, ['position' => 'ASC']);
/** @var $route Route */
$routes = $this->getRouteRepository()->findByStaticPrefix($candidates, ['position' => 'ASC']); /** @phpstan-ignore-line */
/** @var Route $route */
foreach ($routes as $route) {
$collection->add($route->getName(), $route);
}
Expand Down
14 changes: 14 additions & 0 deletions src/Doctrine/Phpcr/ContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr;

use Doctrine\ODM\PHPCR\DocumentManagerInterface;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\DoctrineProvider;
use Symfony\Cmf\Component\Routing\ContentRepositoryInterface;

Expand Down Expand Up @@ -48,4 +49,17 @@ public function getContentId($content): ?string
return null;
}
}

/**
* Make sure the manager is a PHPCR-ODM manager.
*/
protected function getObjectManager(): DocumentManagerInterface
{
$dm = parent::getObjectManager();
if (!$dm instanceof DocumentManagerInterface) {
throw new \LogicException(sprintf('Expected %s, got %s', DocumentManagerInterface::class, get_class($dm)));
}

return $dm;
}
}
17 changes: 14 additions & 3 deletions src/Doctrine/Phpcr/RouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\ODM\PHPCR\DocumentManager;
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use PHPCR\RepositoryException;
use PHPCR\Util\UUIDHelper;
Expand Down Expand Up @@ -85,7 +86,6 @@ public function getRouteCollectionForRequest(Request $request): RouteCollection
}

try {
/** @var $dm DocumentManager */
$dm = $this->getObjectManager();
$routes = $dm->findMany($this->className, $candidates);
// filter for valid route objects
Expand Down Expand Up @@ -151,7 +151,6 @@ private function getAllRoutes(): iterable
}

try {
/** @var $dm DocumentManager */
$dm = $this->getObjectManager();
} catch (RepositoryException $e) {
// special case: there is not even a database existing. this means there are no routes.
Expand Down Expand Up @@ -192,7 +191,6 @@ public function getRoutesByNames(?array $names = null): iterable
return [];
}

/** @var $dm DocumentManager */
$dm = $this->getObjectManager();
$documents = $dm->findMany($this->className, $candidates);
foreach ($documents as $key => $document) {
Expand All @@ -210,4 +208,17 @@ public function getRoutesByNames(?array $names = null): iterable

return $documents;
}

/**
* Make sure the manager is a PHPCR-ODM manager.
*/
protected function getObjectManager(): DocumentManagerInterface
{
$dm = parent::getObjectManager();
if (!$dm instanceof DocumentManagerInterface) {
throw new \LogicException(sprintf('Expected %s, got %s', DocumentManagerInterface::class, get_class($dm)));
}

return $dm;
}
}
3 changes: 3 additions & 0 deletions src/Validator/Constraints/RouteDefaultsTwigValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public function __construct(ControllerResolverInterface $controllerResolver, ?Lo

public function validate($defaults, Constraint $constraint)
{
if (!$constraint instanceof RouteDefaults) {
throw new \InvalidArgumentException(sprintf('Expected %s, got %s', RouteDefaults::class, get_class($constraint)));
}
if (\array_key_exists('_controller', $defaults) && null !== $defaults['_controller']) {
$controller = $defaults['_controller'];

Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/App/DataFixtures/Phpcr/LoadRouteData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\ODM\PHPCR\Document\Generic;
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
use Doctrine\Persistence\ObjectManager;
use PHPCR\Util\NodeHelper;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RedirectRoute;
Expand All @@ -22,6 +23,9 @@ class LoadRouteData implements FixtureInterface
{
public function load(ObjectManager $manager)
{
if (!$manager instanceof DocumentManagerInterface) {
throw new \InvalidArgumentException(sprintf('Expected %s, got %s', DocumentManagerInterface::class, get_class($manager)));
}
NodeHelper::createPath($manager->getPhpcrSession(), '/test');

$root = $manager->find(null, '/test');
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/App/Document/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Content
#[PHPCRODM\ParentDocument]
private $parent;

#[PHPCRODM\NodeName]
#[PHPCRODM\Nodename]
private $name;

#[PHPCRODM\Field(type: 'string')]
Expand Down
7 changes: 4 additions & 3 deletions tests/Fixtures/App/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Fixtures\App;

use Symfony\Cmf\Bundle\ResourceBundle\CmfResourceBundle;
use Symfony\Cmf\Bundle\ResourceRestBundle\CmfResourceRestBundle;
use Symfony\Cmf\Component\Testing\HttpKernel\TestKernel;
use Symfony\Component\Config\Loader\LoaderInterface;
Expand All @@ -31,10 +32,10 @@ public function configure(): void

$this->registerConfiguredBundles();

if (class_exists(CmfResourceRestBundle::class)) {
if (class_exists(CmfResourceBundle::class) && class_exists(CmfResourceRestBundle::class)) {
$this->addBundles([
new \Symfony\Cmf\Bundle\ResourceBundle\CmfResourceBundle(),
new \Symfony\Cmf\Bundle\ResourceRestBundle\CmfResourceRestBundle(),
new CmfResourceBundle(),
new CmfResourceRestBundle(),
]);
}
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Functional/Doctrine/Phpcr/RedirectRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ public function testRedirectDoctrine()
$route = $this->getDm()->find(null, self::ROUTE_ROOT.'/testroute');
$redirect = $this->getDm()->find(null, self::ROUTE_ROOT.'/redirect');

$this->assertInstanceOf(RedirectRouteInterface::class, $redirect);
$this->assertInstanceOf(RedirectRoute::class, $redirect);
$this->assertSame($redirect, $redirect->getContent());
$params = $redirect->getParameters();
$this->assertSame($route, $redirect->getRouteTarget());
$defaults = $redirect->getDefaults();
$this->assertEquals(['test' => 'toast'], $defaults);
Expand Down

This file was deleted.

3 changes: 1 addition & 2 deletions tests/Unit/Doctrine/Phpcr/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ public function testGetRouteChildren(): void
{
$refl = new \ReflectionClass($this->route);
$prop = $refl->getProperty('children');
$prop->setAccessible(true);
$prop->setValue($this->route, new ArrayCollection([
new \stdClass(),
$this->childRoute1,
]));

$res = $this->route->getRouteChildren();
$this->assertCount(1, $res);
$this->assertEquals('child route1', $res[0]->getName());
$this->assertSame($this->childRoute1, $res[0]);
}

public function testGetRouteChildrenNull(): void
Expand Down

This file was deleted.

Loading

0 comments on commit 9341e85

Please sign in to comment.