Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PHPStan #468

Merged
merged 7 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ jobs:
- name: Validate composer.json
run: composer validate --strict --no-check-lock

static_analysis:
name: Static analysis
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: shivammathur/setup-php@v2
with:
coverage: none
php-version: '8.2'

- name: Install dependencies
run: composer update --ansi --no-progress --prefer-dist --no-interaction

- name: Install PHPUnit
run: vendor/bin/simple-phpunit install

- run: vendor/bin/phpstan analyze

tests:
name: "Tests on PHP ${{ matrix.php }}${{ matrix.name_suffix }}"
runs-on: ubuntu-latest
Expand Down
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
"gedmo/doctrine-extensions": "^3.5.0"
},
"require-dev": {
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-strict-rules": "^1.5",
"phpstan/phpstan-symfony": "^1.3",
"symfony/mime": "^5.4 || ^6.0 || ^7.0",
"symfony/phpunit-bridge": "^v6.4.1 || ^7.0.1",
"symfony/security-core": "^5.4 || ^6.0 || ^7.0"
Expand All @@ -40,5 +45,8 @@
"branch-alias": {
"dev-main": "1.x-dev"
}
},
"config": {
"sort-packages": true
}
}
13 changes: 13 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
parameters:
level: max
paths:
- src/
- tests/

includes:
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-symfony/rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Stof\DoctrineExtensionsBundle\DependencyInjection\Compiler;

use Stof\DoctrineExtensionsBundle\DependencyInjection\StofDoctrineExtensionsExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

Expand All @@ -23,6 +24,9 @@ class ValidateExtensionConfigurationPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
$container->getExtension('stof_doctrine_extensions')->configValidate($container);
$extension = $container->getExtension('stof_doctrine_extensions');
\assert($extension instanceof StofDoctrineExtensionsExtension);

$extension->configValidate($container);
}
}
16 changes: 9 additions & 7 deletions src/DependencyInjection/StofDoctrineExtensionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class StofDoctrineExtensionsExtension extends Extension
),
);

/** @var list<string> */
private $entityManagers = array();
/** @var list<string> */
private $documentManagers = array();

/**
Expand Down Expand Up @@ -150,7 +152,7 @@ public function load(array $configs, ContainerBuilder $container)
/**
* @internal
*/
public function configValidate(ContainerBuilder $container)
public function configValidate(ContainerBuilder $container): void
{
foreach ($this->entityManagers as $name) {
if (!$container->hasDefinition(sprintf('doctrine.dbal.%s_connection', $name))) {
Expand All @@ -166,13 +168,13 @@ public function configValidate(ContainerBuilder $container)
}

/**
* @param array $configs
* @param ContainerBuilder $container
* @param LoaderInterface $loader
* @param array $loaded
* @param string $doctrineListenerTag
* @param array<string, array<string, bool>> $configs
* @param ContainerBuilder $container
* @param LoaderInterface $loader
* @param array<string, true> $loaded
* @param string $doctrineListenerTag
*
* @return array
* @return list<string>
*/
private function processObjectManagerConfigurations(array $configs, ContainerBuilder $container, LoaderInterface $loader, array &$loaded, string $doctrineListenerTag)
{
Expand Down
8 changes: 4 additions & 4 deletions src/EventListener/BlameListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
*/
class BlameListener implements EventSubscriberInterface
{
/** @var AuthorizationCheckerInterface|null */
private $authorizationChecker;
/** @var TokenStorageInterface|null */
private $tokenStorage;
/** @var BlameableListener */
private $blameableListener;

public function __construct(BlameableListener $blameableListener, TokenStorageInterface $tokenStorage = null, AuthorizationCheckerInterface $authorizationChecker = null)
Expand All @@ -32,7 +35,7 @@ public function __construct(BlameableListener $blameableListener, TokenStorageIn
/**
* @internal
*/
public function onKernelRequest(RequestEvent $event)
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
Expand All @@ -48,9 +51,6 @@ public function onKernelRequest(RequestEvent $event)
}
}

/**
* @return string[]
*/
public static function getSubscribedEvents()
{
return array(
Expand Down
3 changes: 2 additions & 1 deletion src/EventListener/IpTraceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
*/
final class IpTraceListener implements EventSubscriberInterface
{
/** @var IpTraceableListener */
private $ipTraceableListener;

public function __construct(IpTraceableListener $ipTraceableListener)
{
$this->ipTraceableListener = $ipTraceableListener;
}

public function onKernelRequest(RequestEvent $event)
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
Expand Down
6 changes: 2 additions & 4 deletions src/EventListener/LocaleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
class LocaleListener implements EventSubscriberInterface
{
/** @var TranslatableListener */
private $translatableListener;

public function __construct(TranslatableListener $translatableListener)
Expand All @@ -24,14 +25,11 @@ public function __construct(TranslatableListener $translatableListener)
/**
* @internal
*/
public function onKernelRequest(RequestEvent $event)
public function onKernelRequest(RequestEvent $event): void
{
$this->translatableListener->setTranslatableLocale($event->getRequest()->getLocale());
}

/**
* @return string[]
*/
public static function getSubscribedEvents()
{
return array(
Expand Down
14 changes: 10 additions & 4 deletions src/EventListener/LoggerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Stof\DoctrineExtensionsBundle\EventListener;

use Gedmo\Loggable\Loggable;
use Gedmo\Loggable\LoggableListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
Expand All @@ -14,13 +15,21 @@
* Sets the username from the security context by listening on kernel.request
*
* @author Christophe Coevoet <stof@notk.org>
*
* @phpstan-template T of Loggable|object
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this right?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loggable|object is the same than object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used the same definition as in LoggableListener, so what should I do here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either leave it as is or just make it T of object. Gedmo\Loggable\Loggable is an optional marker interface folks can use in their applications if they want to mark their models as loggable, but nothing requires implementing it and the only references to it in the library are template annotations.

*/
class LoggerListener implements EventSubscriberInterface
{
/** @var AuthorizationCheckerInterface|null */
private $authorizationChecker;
/** @var TokenStorageInterface|null */
private $tokenStorage;
/** @var LoggableListener<T> */
private $loggableListener;

/**
* @param LoggableListener<T> $loggableListener
*/
public function __construct(LoggableListener $loggableListener, TokenStorageInterface $tokenStorage = null, AuthorizationCheckerInterface $authorizationChecker = null)
{
$this->loggableListener = $loggableListener;
Expand All @@ -31,7 +40,7 @@ public function __construct(LoggableListener $loggableListener, TokenStorageInte
/**
* @internal
*/
public function onKernelRequest(RequestEvent $event)
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
Expand All @@ -48,9 +57,6 @@ public function onKernelRequest(RequestEvent $event)
}
}

/**
* @return string[]
*/
public static function getSubscribedEvents()
{
return array(
Expand Down
8 changes: 8 additions & 0 deletions src/Uploadable/UploadableManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
namespace Stof\DoctrineExtensionsBundle\Uploadable;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Gedmo\Uploadable\FileInfo\FileInfoInterface;
use Gedmo\Uploadable\UploadableListener;

class UploadableManager
{
/** @var \Gedmo\Uploadable\UploadableListener */
private $listener;
/** @var class-string<FileInfoInterface> */
private $fileInfoClass;

/**
* @param UploadableListener $listener
* @param class-string<FileInfoInterface> $fileInfoClass
*/
public function __construct(UploadableListener $listener, $fileInfoClass)
{
$this->listener = $listener;
Expand All @@ -24,6 +30,8 @@ public function __construct(UploadableListener $listener, $fileInfoClass)
*
* @param object $entity - The entity you are marking to "Upload" as soon as you call "flush".
* @param mixed $fileInfo - The file info object or array. In Symfony, this will be typically an UploadedFile instance.
*
* @return void
*/
public function markEntityToUpload($entity, $fileInfo)
{
Expand Down
7 changes: 5 additions & 2 deletions src/Uploadable/UploadedFileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class UploadedFileInfo implements FileInfoInterface
{
/** @var UploadedFile */
private $uploadedFile;

public function __construct(UploadedFile $uploadedFile)
Expand All @@ -31,11 +32,13 @@ public function getName()
}

/**
* @return ?string
* @return int|null
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

covariance with interface

*/
public function getSize()
{
return $this->uploadedFile->getSize();
$size = $this->uploadedFile->getSize();

return $size !== false ? $size : null;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Uploadable/ValidatorConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
class ValidatorConfigurator
{
/** @var bool */
private $validateWritableDirectory;

/**
Expand All @@ -19,7 +20,7 @@ public function __construct($validateWritableDirectory)
$this->validateWritableDirectory = $validateWritableDirectory;
}

public function configure()
public function configure(): void
{
Validator::$validateWritableDirectory = $this->validateWritableDirectory;
}
Expand Down
Loading