php-cs-fixer and phpstan

This commit is contained in:
David Buchmann
2024-04-03 10:37:30 +02:00
parent 1081581152
commit b833f68dfb
35 changed files with 145 additions and 73 deletions

47
.github/workflows/static.yml vendored Normal file
View File

@@ -0,0 +1,47 @@
name: Static analysis
on:
push:
branches:
- '[0-9]+.x'
- '[0-9]+.[0-9]+'
- '[0-9]+.[0-9]+.x'
pull_request:
jobs:
phpstan:
name: PHPStan
runs-on: ubuntu-latest
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: "curl,dom,json,xml,dom"
coverage: none
- name: Checkout code
uses: actions/checkout@v3
# have to install phpstan ourselves here, the phpstan-ga fails at composer install with weird errors
- name: Install phpstan
run: |
composer require --no-update jackalope/jackalope-doctrine-dbal jackalope/jackalope-jackrabbit phpstan/phpstan
composer update
- name: PHPStan
run: vendor/bin/phpstan analyze --no-progress
php-cs-fixer:
name: PHP-CS-Fixer
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --dry-run --diff

View File

@@ -20,18 +20,14 @@ jobs:
matrix:
include:
- php-version: "8.1"
phpunit-version: "9.5"
phpunit-flags: "-v --coverage-text"
symfony-version: "6.0.*"
- php-version: "8.1"
phpunit-version: "9.5"
- php-version: "8.2"
phpunit-version: "9.5"
- php-version: "8.3"
phpunit-version: "9.5"
symfony-version: "7.*"
steps:
@@ -65,8 +61,4 @@ jobs:
run: "tests/phpcr_odm_doctrine_dbal.sh"
- name: "Execute test cases"
run: |
if [[ $SYMFONY_PHPUNIT_VERSION == '' ]]; then unset SYMFONY_PHPUNIT_VERSION; fi;
vendor/bin/simple-phpunit ${{ matrix.phpunit-flags }}
env:
SYMFONY_PHPUNIT_VERSION: "${{ matrix.phpunit-version }}"
run: vendor/bin/phpunit ${{ matrix.phpunit-flags }}

6
.gitignore vendored
View File

@@ -1,5 +1,7 @@
composer.lock
vendor
phpunit.xml
!tests/Fixtures/App/var/.gitempty
tests/Fixtures/App/var
/.php_cs.cache
vendor
.phpunit.result.cache
.php-cs-fixer.cache

19
.php-cs-fixer.dist.php Normal file
View File

@@ -0,0 +1,19 @@
<?php
$finder = PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
->in(__DIR__.'/tests')
->exclude(__DIR__.'/tests/Fixtures/App/var')
->name('*.php')
;
$config = new PhpCsFixer\Config();
return $config
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'single_line_throw' => false,
])
->setFinder($finder)
;

View File

@@ -1,9 +0,0 @@
<?php
require_once __DIR__.'/vendor/autoload.php';
use SLLH\StyleCIBridge\ConfigBridge;
return ConfigBridge::create()
->setUsingCache(true)
;

View File

@@ -43,6 +43,12 @@
"doctrine/orm": "^2.0 || ^3.0",
"jackalope/jackalope-doctrine-dbal": "^2.0",
"matthiasnoback/symfony-dependency-injection-test": "^4.3.1 || ^5.0",
"phpcr/phpcr-shell": "^1.6",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-doctrine": "^1.3",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-symfony": "^1.3",
"phpunit/phpunit": "^9.5",
"symfony/asset": "^5.4 || ^6.0 || ^7.0",
"symfony/browser-kit": "^5.4 || ^6.0 || ^7.0",
"symfony/css-selector": "^5.4 || ^6.0 || ^7.0",
@@ -50,7 +56,7 @@
"symfony/form": "^5.4 || ^6.0 || ^7.0",
"symfony/monolog-bridge": "^5.4 || ^6.0 || ^7.0",
"symfony/monolog-bundle": "^3.4",
"symfony/phpunit-bridge": "^v6.4.2 || ^v7.0.2",
"symfony/stopwatch": "^5.4 || ^6.0 || ^7.0",
"symfony/templating": "^5.4 || ^6.0 || ^7.0",
"symfony/translation": "^5.4 || ^6.0 || ^7.0",
"symfony/twig-bundle": "^5.4 || ^6.0 || ^7.0",

15
phpstan.neon.dist Normal file
View File

@@ -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/fixtures/config/
- tests/Fixtures/App/config/

View File

@@ -29,13 +29,13 @@ abstract class DoctrineCommandHelper
{
$connectionService = sprintf('doctrine_phpcr.jackalope_doctrine_dbal.%s_connection', $sessionName);
$helperSet = $application->getHelperSet();
$helperSet->set(new DoctrineDBALHelper($application->getKernel()->getContainer()->get($connectionService)));
$helperSet->set(new DoctrineDbalHelper($application->getKernel()->getContainer()->get($connectionService)));
}
/**
* Prepare the DBAL connection and the PHPCR session.
*/
public static function setApplicationPHPCRSession(Application $application, string $sessionName = null, bool $admin = false): void
public static function setApplicationPHPCRSession(Application $application, ?string $sessionName = null, bool $admin = false): void
{
$registry = self::getRegistry($application);
$session = $admin ? $registry->getAdminConnection($sessionName) : $registry->getConnection($sessionName);
@@ -52,7 +52,7 @@ abstract class DoctrineCommandHelper
|| $session->getTransport() instanceof DbalLoggingClient
)
) {
$helperSet->set(new DoctrineDBALHelper($session->getTransport()->getConnection()));
$helperSet->set(new DoctrineDbalHelper($session->getTransport()->getConnection()));
}
}

View File

@@ -8,7 +8,6 @@ use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Wrapper to use this command in the symfony console with multiple sessions.

View File

@@ -75,7 +75,7 @@ EOT
$launchShell = empty($args);
$session = $this->getPhpcrSession();
// If no arguments supplied, launch the shell uwith the embedded application
// If no arguments supplied, launch the shell with the embedded application
if ($launchShell) {
$shell = PhpcrShell::createEmbeddedShell($session);
$exitCode = $shell->run();

View File

@@ -38,7 +38,7 @@ final class PHPCRDataCollector extends DataCollector
$this->managers = $registry->getManagerNames();
}
public function collect(Request $request, Response $response, \Throwable $exception = null): void
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
{
$this->collectInternal($request, $response);
}
@@ -114,8 +114,6 @@ final class PHPCRDataCollector extends DataCollector
* The return value is an array with the sanitized value and a boolean
* indicating if the original value was kept (allowing to use the sanitized
* value to explain the call).
*
* @param mixed $var
*/
private function sanitizeParam($var): array
{
@@ -155,7 +153,7 @@ final class PHPCRDataCollector extends DataCollector
return [$var, true];
}
protected function collectInternal(Request $request, Response $response, \Throwable $exception = null): void
protected function collectInternal(Request $request, Response $response, ?\Throwable $exception = null): void
{
$calls = [];
foreach ($this->loggers as $name => $logger) {

View File

@@ -18,7 +18,7 @@ final class StopWatchLogger implements LoggerInterface
$this->stopwatch = $stopwatch;
}
public function startCall($method, array $params = null, array $env = null): void
public function startCall($method, ?array $params = null, ?array $env = null): void
{
if (null !== $this->stopwatch) {
$this->stopwatch->start('doctrine_phpcr', 'doctrine_phpcr');

View File

@@ -18,8 +18,8 @@ final class PHPCRExecutor extends BasePHPCRExecutor
public function __construct(
DocumentManagerInterface $dm,
PHPCRPurger $purger = null,
InitializerManager $initializerManager = null
?PHPCRPurger $purger = null,
?InitializerManager $initializerManager = null
) {
parent::__construct($dm, $purger);

View File

@@ -2,12 +2,12 @@
namespace Doctrine\Bundle\PHPCRBundle\DependencyInjection;
use Jackalope\Tools\Console\Command\InitDoctrineDbalCommand as BaseInitDoctrineDbalCommand;
use Jackalope\Tools\Console\Command\JackrabbitCommand as BaseJackrabbitCommand;
use Doctrine\Bundle\PHPCRBundle\ManagerRegistryInterface;
use Doctrine\ODM\PHPCR\Document\Generic;
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
use Jackalope\Session;
use Jackalope\Tools\Console\Command\InitDoctrineDbalCommand as BaseInitDoctrineDbalCommand;
use Jackalope\Tools\Console\Command\JackrabbitCommand as BaseJackrabbitCommand;
use PHPCR\SessionInterface;
use Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
@@ -71,7 +71,7 @@ final class DoctrinePHPCRExtension extends AbstractDoctrineExtension
$this->loader->load('jackalope_doctrine_dbal-commands.xml');
}
// default values in case no odm is configured. the manager registry needs these variables to be defined.
// default values in case no odm is configured. the manager registry needs these variables to be defined.
// if odm is enabled, the parameters are overwritten later in the `loadOdm` section.
$container->setParameter('doctrine_phpcr.odm.document_managers', []);
$container->setParameter('doctrine_phpcr.odm.default_document_manager', '');
@@ -588,7 +588,7 @@ final class DoctrinePHPCRExtension extends AbstractDoctrineExtension
return 'Document';
}
protected function getMappingResourceConfigDirectory(string $bundleDir = null): string
protected function getMappingResourceConfigDirectory(?string $bundleDir = null): string
{
return 'Resources/config/doctrine';
}

View File

@@ -24,7 +24,7 @@ final class PhpcrOdmQueryBuilderLoader implements EntityLoaderInterface
/**
* @param QueryBuilder|\Closure $queryBuilder
*/
public function __construct($queryBuilder, DocumentManagerInterface $manager = null, string $class = null)
public function __construct($queryBuilder, ?DocumentManagerInterface $manager = null, ?string $class = null)
{
// If a query builder was passed, it must be a closure or QueryBuilder
// instance

View File

@@ -67,9 +67,9 @@ final class PhpcrOdmTypeGuesser implements FormTypeGuesserInterface
case 'referrers':
return new TypeGuess(DocumentType::class, [
'class' => $mapping['referringDocument'],
'multiple' => true,
],
'class' => $mapping['referringDocument'],
'multiple' => true,
],
Guess::HIGH_CONFIDENCE
);

View File

@@ -22,7 +22,7 @@ class PHPCRReferenceType extends AbstractType
{
private ?SessionInterface $session;
public function __construct(SessionInterface $session = null)
public function __construct(?SessionInterface $session = null)
{
$this->session = $session;
}

View File

@@ -29,7 +29,7 @@ class InitializerManager
$this->registry = $registry;
}
public function setLoggingClosure(\Closure $closure = null): void
public function setLoggingClosure(?\Closure $closure = null): void
{
$this->loggingClosure = $closure;
}
@@ -52,7 +52,7 @@ class InitializerManager
/**
* Iterate over the registered initializers and execute each of them.
*/
public function initialize(string $sessionName = null): void
public function initialize(?string $sessionName = null): void
{
$loggingClosure = $this->loggingClosure;

View File

@@ -3,10 +3,9 @@
namespace Doctrine\Bundle\PHPCRBundle;
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
use Doctrine\ODM\PHPCR\PHPCRException;
use PHPCR\SessionInterface;
use Psr\Container\ContainerInterface;
use Symfony\Bridge\Doctrine\ManagerRegistry as BaseManagerRegistry;
use Symfony\Component\DependencyInjection\Container;
/**
* Symfony aware manager registry.
@@ -18,7 +17,7 @@ final class ManagerRegistry extends BaseManagerRegistry implements ManagerRegist
* @param string[] $entityManagers
*/
public function __construct(
ContainerInterface $container,
Container $container,
array $connections,
array $entityManagers,
string $defaultConnectionName,

View File

@@ -21,6 +21,7 @@ class JackrabbitCommand extends BaseJackrabbitCommand
) {
parent::__construct(self::NAME);
}
protected function configure(): void
{
parent::configure();

View File

@@ -8,7 +8,6 @@ use Doctrine\Bundle\PHPCRBundle\Initializer\InitializerManager;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Purger\PHPCRPurger;
use Doctrine\ODM\PHPCR\Tools\Console\Helper\DocumentManagerHelper;
use InvalidArgumentException;
use PHPCR\Util\Console\Command\BaseCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
@@ -117,7 +116,7 @@ EOT
$fixtures = $loader->getFixtures();
if (!$fixtures) {
throw new InvalidArgumentException(
throw new \InvalidArgumentException(
sprintf('Could not find any fixtures to load in: %s', "\n\n- ".implode("\n- ", $paths))
);
}

View File

@@ -29,7 +29,7 @@ class RepositoryManager
return $this->managerRegistry;
}
public function getDocumentManager(string $managerName = null): DocumentManagerInterface
public function getDocumentManager(?string $managerName = null): DocumentManagerInterface
{
return $this->getRegistry()->getManager($managerName);
}

View File

@@ -27,6 +27,9 @@ class ValidPhpcrOdmValidator extends ConstraintValidator
*/
public function validate($document, Constraint $constraint): void
{
if (!$constraint instanceof ValidPhpcrOdm) {
throw new \InvalidArgumentException('Expected a constraint of class '.ValidPhpcrOdm::class);
}
$className = \get_class($document);
$dm = $this->registry->getManagerForClass($className);

View File

@@ -15,13 +15,16 @@ use Doctrine\Bundle\PHPCRBundle\Tests\Fixtures\App\Document\ReferrerDocument;
use Doctrine\Bundle\PHPCRBundle\Tests\Fixtures\App\Document\TestDocument;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\ODM\PHPCR\Document\Generic;
use Doctrine\ODM\PHPCR\DocumentManager;
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
use Doctrine\Persistence\ObjectManager;
class LoadData implements FixtureInterface
{
public function load(ObjectManager $manager): void
{
if (!$manager instanceof DocumentManagerInterface) {
throw new \InvalidArgumentException('Expected a '.DocumentManagerInterface::class);
}
$base = new Generic();
$base->setNodename('test');
$base->setParentDocument($manager->find(null, '/'));

View File

@@ -44,17 +44,11 @@ class ReferrerDocument
$this->documents->add($doc);
}
/**
* @return mixed
*/
public function getSingle()
{
return $this->single;
}
/**
* @return mixed
*/
public function getTestDocument()
{
return $this->testDocument;
@@ -68,9 +62,6 @@ class ReferrerDocument
return $this->testDocuments;
}
/**
* @return mixed
*/
public function getDocuments()
{
return $this->documents;

View File

@@ -1,10 +1,10 @@
<?php
return [
\Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
\Doctrine\Bundle\PHPCRBundle\DoctrinePHPCRBundle::class => ['all' => true],
\Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
\Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
\Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
\Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
Doctrine\Bundle\PHPCRBundle\DoctrinePHPCRBundle::class => ['all' => true],
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['all' => true],
];

View File

@@ -27,7 +27,7 @@ class NodeDumpCommandTest extends BaseTestCase
'command' => $command->getName(),
'--props' => true,
'--max_line_length' => 120,
'identifier' => '/test/doc-very-long',
'identifier' => '/test/doc-very-long',
]);
$output = $commandTester->getDisplay();

View File

@@ -7,6 +7,7 @@ use Doctrine\Bundle\PHPCRBundle\Tests\Fixtures\App\DataFixtures\PHPCR\LoadData;
use Doctrine\Bundle\PHPCRBundle\Tests\Fixtures\App\Document\TestDocument;
use Doctrine\Bundle\PHPCRBundle\Tests\Functional\BaseTestCase;
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder;
class PhpcrOdmQueryBuilderLoaderTest extends BaseTestCase
{
@@ -46,6 +47,7 @@ class PhpcrOdmQueryBuilderLoaderTest extends BaseTestCase
public function testGetByIdsFilter(): void
{
$qb = $this->dm->getRepository(TestDocument::class)->createQueryBuilder('e');
$this->assertInstanceOf(QueryBuilder::class, $qb);
$qb->where()->eq()->field('e.text')->literal('thiswillnotmatch');
$loader = new PhpcrOdmQueryBuilderLoader($qb, $this->dm);
$documents = $loader->getEntitiesByIds('id', ['/test/doc']);

View File

@@ -8,6 +8,7 @@ use Doctrine\Bundle\PHPCRBundle\Tests\Fixtures\App\Document\ReferrerDocument;
use Doctrine\Bundle\PHPCRBundle\Tests\Fixtures\App\Document\TestDocument;
use Doctrine\Bundle\PHPCRBundle\Tests\Functional\BaseTestCase;
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormBuilderInterface;
use Twig\Environment;
@@ -68,6 +69,7 @@ class DocumentTypeTest extends BaseTestCase
->getRepository(TestDocument::class)
->createQueryBuilder('e')
;
$this->assertInstanceOf(QueryBuilder::class, $qb);
$qb->where()->eq()->field('e.text')->literal('thiswillnotmatch');
$formBuilder = $this->createFormBuilder($this->referrer);

View File

@@ -10,7 +10,7 @@ use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Exception\TransformationFailedException;
class DocumentToPathTransformerTest extends Testcase
class DocumentToPathTransformerTest extends TestCase
{
/**
* @var DocumentManager&MockObject

View File

@@ -9,7 +9,7 @@ use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
class PHPCRNodeToPathTransformerTest extends Testcase
class PHPCRNodeToPathTransformerTest extends TestCase
{
/**
* @var SessionInterface&MockObject

View File

@@ -8,7 +8,7 @@ use PHPCR\SessionInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class PHPCRNodeToUuidTransformerTest extends Testcase
class PHPCRNodeToUuidTransformerTest extends TestCase
{
/**
* @var SessionInterface&MockObject

View File

@@ -11,7 +11,7 @@ use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Form\FormBuilder;
class PHPCRReferenceTypeTest extends Testcase
class PHPCRReferenceTypeTest extends TestCase
{
/**
* @var FormBuilder&MockObject

View File

@@ -11,7 +11,7 @@ use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormBuilder;
class PathTypeTest extends Testcase
class PathTypeTest extends TestCase
{
/**
* @var ManagerRegistryInterface&MockObject

View File

@@ -13,7 +13,10 @@ use PHPUnit\Framework\TestCase;
class GenericInitializerTest extends TestCase
{
protected ManagerRegistryInterface $registry;
/**
* @var ManagerRegistryInterface&MockObject
*/
private ManagerRegistryInterface $registry;
/**
* @var SessionInterface&MockObject
*/