Skip to content

Commit

Permalink
Upgrade to PHPStan 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Jan 14, 2025
1 parent ea6959d commit 9763aa2
Show file tree
Hide file tree
Showing 83 changed files with 930 additions and 529 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
"doctrine/orm": "^2.14.0 || ^3.0",
"friendsofphp/php-cs-fixer": "^3.14.0",
"nesbot/carbon": "^2.71 || ^3.0",
"phpstan/phpstan": "^1.11",
"phpstan/phpstan-doctrine": "^1.4",
"phpstan/phpstan-phpunit": "^1.4",
"phpstan/phpstan": "^2.1.1",
"phpstan/phpstan-doctrine": "^2.0.1",
"phpstan/phpstan-phpunit": "^2.0.3",
"phpunit/phpunit": "^9.6",
"rector/rector": "^1.1",
"rector/rector": "^2.0.6",
"symfony/console": "^5.4 || ^6.0 || ^7.0",
"symfony/doctrine-bridge": "^5.4 || ^6.0 || ^7.0",
"symfony/phpunit-bridge": "^6.0 || ^7.0",
Expand Down
759 changes: 546 additions & 213 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ parameters:
- tests/bootstrap.php
treatPhpDocTypesAsCertain: false
ignoreErrors:
-
identifier: trait.unused
- '#^Property Gedmo\\Tests\\.+\\Fixture\\[^:]+::\$\w+ is never written, only read\.$#'
- '#^Property Gedmo\\Tests\\.+\\Fixture\\[^:]+::\$\w+ is never read, only written\.$#'
- '#^Property Gedmo\\Tests\\.+\\Fixture\\[^:]+::\$\w+ is unused\.$#'
- '#^Method Gedmo\\(?:[^\\]+\\)*Mapping\\Driver[^:]+::readExtendedMetadata\(\) with return type void returns [\w\|<>\s,]+ but should not return anything\.$#'
- '#^Method Gedmo\\Uploadable\\Mapping\\Validator::validateConfiguration\(\) with return type void returns array<string, mixed> but should not return anything\.$#'
- '#^Result of static method Gedmo\\Uploadable\\Mapping\\Validator::validateConfiguration\(\) \(void\) is used\.$#'
- '#^Result of method Gedmo\\Mapping\\Driver::readExtendedMetadata\(\) \(void\) is used\.$#'
excludePaths:
# Deprecated and unused class, interface does not exist as of 4.0
Expand Down
20 changes: 10 additions & 10 deletions src/AbstractTrackingListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
/**
* The AbstractTrackingListener provides generic functions for all listeners.
*
* @phpstan-template TConfig of array
* @phpstan-template TEventAdapter of AdapterInterface
* @template TConfig of array
* @template TEventAdapter of AdapterInterface
*
* @phpstan-extends MappedEventSubscriber<TConfig, TEventAdapter>
* @template-extends MappedEventSubscriber<TConfig, TEventAdapter>
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
*/
Expand Down Expand Up @@ -207,9 +207,9 @@ public function prePersist(EventArgs $args)
/**
* Get the value for an updated field.
*
* @param ClassMetadata $meta
* @param string $field
* @param AdapterInterface $eventAdapter
* @param ClassMetadata<object> $meta
* @param string $field
* @param TEventAdapter $eventAdapter
*
* @return mixed
*/
Expand All @@ -218,10 +218,10 @@ abstract protected function getFieldValue($meta, $field, $eventAdapter);
/**
* Updates a field.
*
* @param object $object
* @param AdapterInterface $eventAdapter
* @param ClassMetadata $meta
* @param string $field
* @param object $object
* @param TEventAdapter $eventAdapter
* @param ClassMetadata<object> $meta
* @param string $field
*
* @return void
*/
Expand Down
5 changes: 3 additions & 2 deletions src/Blameable/BlameableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class BlameableListener extends AbstractTrackingListener
/**
* Get the user value to set on a blameable field
*
* @param ClassMetadata $meta
* @param string $field
* @param ClassMetadata<object> $meta
* @param string $field
* @param BlameableAdapter $eventAdapter
*
* @return mixed
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Blameable/Mapping/Driver/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public function readExtendedMetadata($meta, array &$config)
/**
* Checks if $field type is valid
*
* @param ClassMetadata $meta
* @param string $field
* @param ClassMetadata<object> $meta
* @param string $field
*
* @return bool
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Blameable/Mapping/Driver/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ protected function _loadMappingFile($file)
/**
* Checks if $field type is valid
*
* @param ClassMetadata $meta
* @param string $field
* @param ClassMetadata<object> $meta
* @param string $field
*
* @return bool
*/
Expand Down
7 changes: 3 additions & 4 deletions src/IpTraceable/IpTraceableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Gedmo\AbstractTrackingListener;
use Gedmo\Exception\InvalidArgumentException;
use Gedmo\IpTraceable\Mapping\Event\IpTraceableAdapter;
use Gedmo\Mapping\Event\AdapterInterface;

/**
* The IpTraceable listener handles the update of
Expand All @@ -35,9 +34,9 @@ class IpTraceableListener extends AbstractTrackingListener
/**
* Get the ipValue value to set on a ip field
*
* @param ClassMetadata $meta
* @param string $field
* @param AdapterInterface $eventAdapter
* @param ClassMetadata<object> $meta
* @param string $field
* @param IpTraceableAdapter $eventAdapter
*
* @return string|null
*/
Expand Down
4 changes: 2 additions & 2 deletions src/IpTraceable/Mapping/Driver/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public function readExtendedMetadata($meta, array &$config)
/**
* Checks if $field type is valid
*
* @param ClassMetadata $meta
* @param string $field
* @param ClassMetadata<object> $meta
* @param string $field
*
* @return bool
*/
Expand Down
4 changes: 2 additions & 2 deletions src/IpTraceable/Mapping/Driver/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ protected function _loadMappingFile($file)
/**
* Checks if $field type is valid
*
* @param ClassMetadata $meta
* @param string $field
* @param ClassMetadata<object> $meta
* @param string $field
*
* @return bool
*/
Expand Down
33 changes: 12 additions & 21 deletions src/Loggable/Entity/Repository/LogEntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,25 @@
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
*
* @phpstan-template T of Loggable|object
* @template T of Loggable|object
*
* @phpstan-extends EntityRepository<AbstractLogEntry<T>>
* @template-extends EntityRepository<AbstractLogEntry<T>>
*/
class LogEntryRepository extends EntityRepository
{
/**
* Currently used loggable listener
*
* @phpstan-var LoggableListener<T>|null
* @var LoggableListener<T>|null
*/
private ?LoggableListener $listener = null;

/**
* Loads all log entries for the given entity
*
* @param object $entity
* @param T $entity
*
* @return AbstractLogEntry[]
*
* @phpstan-param T $entity
*
* @phpstan-return array<array-key, AbstractLogEntry<T>>
* @return array<array-key, AbstractLogEntry<T>>
*/
public function getLogEntries($entity)
{
Expand All @@ -57,11 +53,9 @@ public function getLogEntries($entity)
/**
* Get the query for loading of log entries
*
* @param object $entity
* @param T $entity
*
* @return Query
*
* @phpstan-param T $entity
*/
public function getLogEntriesQuery($entity)
{
Expand Down Expand Up @@ -89,14 +83,12 @@ public function getLogEntriesQuery($entity)
* After this operation you will need to
* persist and flush the $entity.
*
* @param object $entity
* @param int $version
* @param T $entity
* @param int $version
*
* @throws UnexpectedValueException
*
* @return void
*
* @phpstan-param T $entity
*/
public function revert($entity, $version = 1)
{
Expand Down Expand Up @@ -151,12 +143,11 @@ public function revert($entity, $version = 1)
}

/**
* @param string $field
* @param mixed $value
* @param ClassMetadata<T> $objectMeta
* @param string $field
* @param mixed $value
*
* @return void
*
* @phpstan-param ClassMetadata<T> $objectMeta
*/
protected function mapValue(ClassMetadata $objectMeta, $field, &$value)
{
Expand All @@ -173,7 +164,7 @@ protected function mapValue(ClassMetadata $objectMeta, $field, &$value)
*
* @throws RuntimeException if listener is not found
*
* @phpstan-return LoggableListener<T>
* @return LoggableListener<T>
*/
private function getLoggableListener(): LoggableListener
{
Expand Down
2 changes: 1 addition & 1 deletion src/Loggable/LoggableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* versioned?: string[],
* }
*
* @phpstan-template T of Loggable|object
* @template T of Loggable|object
*
* @phpstan-extends MappedEventSubscriber<LoggableConfiguration, LoggableAdapter>
*/
Expand Down
16 changes: 10 additions & 6 deletions src/Loggable/Mapping/Driver/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Gedmo\Loggable\Mapping\Driver;

use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as ClassMetadataODM;
use Doctrine\ORM\Mapping\ClassMetadata as ClassMetadataORM;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Gedmo\Exception\InvalidMappingException;
use Gedmo\Mapping\Annotation\Loggable;
Expand Down Expand Up @@ -38,7 +39,7 @@ class Attribute extends AbstractAnnotationDriver

public function validateFullMetadata(ClassMetadata $meta, array $config)
{
if ($config && $meta instanceof ClassMetadataODM && is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
if ($config && $meta instanceof ClassMetadataODM && count($meta->getIdentifier()) > 1) {

Check warning on line 42 in src/Loggable/Mapping/Driver/Attribute.php

View check run for this annotation

Codecov / codecov/patch

src/Loggable/Mapping/Driver/Attribute.php#L42

Added line #L42 was not covered by tests
throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->getName()}");
}

Expand Down Expand Up @@ -94,7 +95,7 @@ public function readExtendedMetadata($meta, array &$config)
}

if (!$meta->isMappedSuperclass && $config) {
if ($meta instanceof ClassMetadataODM && is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
if ($meta instanceof ClassMetadataODM && count($meta->getIdentifier()) > 1) {
throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->getName()}");
}

Expand All @@ -107,7 +108,8 @@ public function readExtendedMetadata($meta, array &$config)
}

/**
* @param string $field
* @param ClassMetadata<object> $meta
* @param string $field
*
* @return bool
*/
Expand All @@ -117,7 +119,8 @@ protected function isMappingValid(ClassMetadata $meta, $field)
}

/**
* @param array<string, mixed> $config
* @param ClassMetadata<object> $meta
* @param array<string, mixed> $config
*
* @return bool
*/
Expand All @@ -129,9 +132,10 @@ protected function isClassAnnotationInValid(ClassMetadata $meta, array &$config)
/**
* Searches properties of embedded objects for versioned fields
*
* @param array<string, mixed> $config
* @param array<string, mixed> $config
* @param ClassMetadataORM<object> $meta
*/
private function inspectEmbeddedForVersioned(string $field, array &$config, \Doctrine\ORM\Mapping\ClassMetadata $meta): void
private function inspectEmbeddedForVersioned(string $field, array &$config, ClassMetadataORM $meta): void
{
$class = new \ReflectionClass($meta->embeddedClasses[$field]['class']);

Expand Down
7 changes: 4 additions & 3 deletions src/Loggable/Mapping/Driver/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function readExtendedMetadata($meta, array &$config)
if (in_array($xmlDoctrine->getName(), ['mapped-superclass', 'entity', 'document'], true)) {
if (isset($xml->loggable)) {
/**
* @var \SimpleXMLElement;
* @var \SimpleXMLElement
*/
$data = $xml->loggable;
$config['loggable'] = true;
Expand Down Expand Up @@ -75,7 +75,7 @@ public function readExtendedMetadata($meta, array &$config)
}

if (!$meta->isMappedSuperclass && $config) {
if ($meta instanceof ClassMetadataODM && is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
if ($meta instanceof ClassMetadataODM && count($meta->getIdentifier()) > 1) {
throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->getName()}");
}
if (isset($config['versioned']) && !isset($config['loggable'])) {
Expand All @@ -89,7 +89,8 @@ public function readExtendedMetadata($meta, array &$config)
/**
* Searches mappings on element for versioned fields
*
* @param array<string, mixed> $config
* @param array<string, mixed> $config
* @param ClassMetadata<object> $meta
*
* @return array<string, mixed>
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Loggable/Mapping/Driver/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function readExtendedMetadata($meta, array &$config)
}

if (!$meta->isMappedSuperclass && $config) {
if ($meta instanceof ClassMetadata && is_array($meta->getIdentifier()) && count($meta->getIdentifier()) > 1) {
if ($meta instanceof ClassMetadata && count($meta->getIdentifier()) > 1) {
throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->getName()}");
}
if (isset($config['versioned']) && !isset($config['loggable'])) {
Expand Down
7 changes: 7 additions & 0 deletions src/Loggable/Mapping/Event/Adapter/ODM.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Gedmo\Loggable\Mapping\Event\Adapter;

use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Gedmo\Loggable\Document\LogEntry;
use Gedmo\Loggable\Mapping\Event\LoggableAdapter;
use Gedmo\Mapping\Event\Adapter\ODM as BaseAdapterODM;
Expand All @@ -26,11 +27,17 @@ public function getDefaultLogEntryClass()
return LogEntry::class;
}

/**
* @param ClassMetadata<object> $meta
*/
public function isPostInsertGenerator($meta)
{
return false;
}

/**
* @param ClassMetadata<object> $meta
*/
public function getNewVersion($meta, $object)
{
$dm = $this->getObjectManager();
Expand Down
5 changes: 4 additions & 1 deletion src/Loggable/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ public function getDefaultLogEntryClass()
}

/**
* @param ClassMetadata $meta
* @param ClassMetadata<object> $meta
*/
public function isPostInsertGenerator($meta)
{
return $meta->idGenerator->isPostInsertGenerator();
}

/**
* @param ClassMetadata<object> $meta
*/
public function getNewVersion($meta, $object)
{
$em = $this->getObjectManager();
Expand Down
Loading

0 comments on commit 9763aa2

Please sign in to comment.