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

PHPStan level 6 with AuditReader not generic #488

Merged
merged 2 commits into from
May 21, 2022

Conversation

franmomu
Copy link
Member

Subject

Alternative to #487 making AuditReader not generic.

Raising PHPStan level after this to level 7 shows 23 errors.

I am targeting this branch, because this is BC.

Changelog

### Added
- Specify iterable types
### Changed
- Make `AuditReader` not generic

@franmomu franmomu added the minor label Apr 23, 2022
@franmomu franmomu mentioned this pull request Apr 23, 2022
@@ -12,3 +12,5 @@ parameters:
checkMissingIterableValueType: true
checkMissingVarTagTypehint: true
checkMissingTypehints: true
ignoreErrors:
- '#Test::(data|provide)[a-zA-Z0-9_]+\(\) return type has no value type specified in iterable type#'
Copy link
Member

Choose a reason for hiding this comment

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

Just a less greedy suggestion:

Suggested change
- '#Test::(data|provide)[a-zA-Z0-9_]+\(\) return type has no value type specified in iterable type#'
- '#^Method SimpleThings\\EntityAudit\\Tests\\[a-zA-Z0-9_\\]+Test::(data|provide)[a-zA-Z0-9_]+\(\) return type has no value type specified in iterable type [a-zA-Z0-9_]+\.$#'

Copy link
Member Author

Choose a reason for hiding this comment

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

Glad to see you back @phansys!

Copy link
Member

Choose a reason for hiding this comment

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

Is there a lot of dataprovider to fix ?

@@ -788,6 +791,8 @@ protected function getEntityPersister($className)
*
* @phpstan-param class-string<T> $className
* @phpstan-return T|null
*
* @template T of object
Copy link
Member

Choose a reason for hiding this comment

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

IMO, we should declare the template before its uses.

@VincentLanglet
Copy link
Member

Removing the template would create issues for the sonata compatibility since there is one in the SonataAdmin interface
https://github.com/sonata-project/SonataAdminBundle/blob/4.x/src/Model/AuditReaderInterface.php#L19

I wonder if we shouldn't have both

/**
 * @phpstan-template T of object
 */
class AuditReader
{
    /**
     * @phpstan-template TT of T
     * @phpstan-param class-string<TT>
     * @phpstan-return TT
     */
    public function find($classname): object
    {
    }
}

This way we can define an AuditReader<Foo|Bar>, but then
if we use find(Foo::class), it will be understood as a Foo object and not only Foo|Bar.

WDYT ?

We should update https://github.com/sonata-project/SonataAdminBundle/blob/4.x/src/Model/AuditReaderInterface.php phpdoc then and the same could be done for the ModelManager https://github.com/sonata-project/SonataAdminBundle/blob/4.x/src/Model/ModelManagerInterface.php#L55

@franmomu
Copy link
Member Author

Removing the template would create issues for the sonata compatibility since there is one in the SonataAdmin interface https://github.com/sonata-project/SonataAdminBundle/blob/4.x/src/Model/AuditReaderInterface.php#L19

I wonder if we shouldn't have both

/**
 * @phpstan-template T of object
 */
class AuditReader
{
    /**
     * @phpstan-template TT of T
     * @phpstan-param class-string<TT>
     * @phpstan-return TT
     */
    public function find($classname): object
    {
    }
}

This way we can define an AuditReader<Foo|Bar>, but then if we use find(Foo::class), it will be understood as a Foo object and not only Foo|Bar.

WDYT ?

IMHO that seems overcomplicated.

I understand that AuditReader::find should be generic and maybe some other methods, but to me this class seems to work more like the EntityManager from doctrine/orm rather than a repository.

@VincentLanglet
Copy link
Member

I understand that AuditReader::find should be generic and maybe some other methods, but to me this class seems to work more like the EntityManager from doctrine/orm rather than a repository.

For SonataAdmin, it's considered as a repository, and the AuditManager would be the EntityManager
https://github.com/sonata-project/SonataAdminBundle/blob/4.x/src/Model/AuditManagerInterface.php#L42-L46
We can use different AuditReader for different classes.

But I think the generic part of the AuditReader of this project can be removed,
this shouldn't break the SonataDoctrineORM static analysis, with this code https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/4.x/src/Model/AuditReader.php#L41-L44

VincentLanglet
VincentLanglet previously approved these changes May 1, 2022
* @param int|string $revision
* @template T of object
*
* @param string $className
Copy link
Member

Choose a reason for hiding this comment

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

isnt this a class-string?

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, all these parameters are defined below as @phpstan-param

* @param int|string $revision
* @template T of object
*
* @param string $className
Copy link
Member

Choose a reason for hiding this comment

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

isnt this a class-string?

@@ -566,15 +566,15 @@ public function findRevisions($className, $id)
* NEXT_MAJOR: Add NoRevisionFoundException as possible exception.
* Gets the current revision of the entity with given ID.
*
* @param string $className
* @param int|string|array $id
* @param string $className
Copy link
Member

Choose a reason for hiding this comment

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

Another class-string?

@@ -508,15 +508,15 @@ public function findRevision($revision)
/**
* Find all revisions that were made of entity class with given id.
*
* @param string $className
* @param int|string|array $id
* @param string $className
Copy link
Member

Choose a reason for hiding this comment

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

And another one

@jordisala1991 jordisala1991 merged commit 1ac3580 into sonata-project:1.x May 21, 2022
@jordisala1991
Copy link
Member

Thanks @franmomu

@franmomu franmomu deleted the phpstan6_audit_not_generic branch May 21, 2022 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants