sonata-project / SonataMediaBundle

Symfony SonataMediaBundle
https://docs.sonata-project.org/projects/SonataMediaBundle
MIT License
449 stars 496 forks source link

phpstan throws setCategory() has invalid type #2344

Closed dmitryuk closed 1 year ago

dmitryuk commented 1 year ago

Environment

Sonata packages

show

``` $ composer show --latest 'sonata-project/*' Direct dependencies required in composer.json: sonata-project/admin-bundle 4.20.0 4.20.0 The missing Symfony Admin Generator sonata-project/doctrine-orm-admin-bundle 4.8.0 4.8.0 Integrate Doctrine ORM into the SonataAdminBundle sonata-project/exporter 2.13.0 3.0.0 Lightweight Exporter library sonata-project/media-bundle 4.5.1 4.5.1 Symfony SonataMediaBundle Transitive dependencies not required in composer.json: sonata-project/block-bundle 4.19.0 4.19.0 Symfony SonataBlockBundle sonata-project/cache 2.2.0 2.2.0 Cache library Package sonata-project/cache is abandoned, you should avoid using it. No replacement was suggested. sonata-project/doctrine-extensions 2.0.1 2.0.1 Doctrine2 behavioral extensions sonata-project/form-extensions 1.18.0 1.18.0 Symfony form extensions sonata-project/twig-extensions 1.12.0 2.0.0 Sonata twig extensions ```

Symfony packages

show

``` Direct dependencies required in composer.json: symfony/monolog-bundle v3.8.0 v3.8.0 Symfony MonologBundle symfony/phpunit-bridge v4.4.47 v6.1.6 Provides utilities for PHPUnit, especially user deprecation notices management symfony/symfony v4.4.47 v6.1.7 The Symfony PHP framework Transitive dependencies not required in composer.json: symfony/contracts v1.1.13 v3.1.1 A set of abstractions extracted out of the Symfony components symfony/deprecation-contracts v3.1.1 v3.1.1 A generic function and convention to trigger deprecation notices symfony/polyfill-ctype v1.26.0 v1.26.0 Symfony polyfill for ctype functions symfony/polyfill-intl-grapheme v1.26.0 v1.26.0 Symfony polyfill for intl's grapheme_* functions symfony/polyfill-intl-icu v1.26.0 v1.26.0 Symfony polyfill for intl's ICU-related data and classes symfony/polyfill-intl-idn v1.26.0 v1.26.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions symfony/polyfill-intl-normalizer v1.26.0 v1.26.0 Symfony polyfill for intl's Normalizer class and related functions symfony/polyfill-mbstring v1.26.0 v1.26.0 Symfony polyfill for the Mbstring extension symfony/polyfill-php72 v1.26.0 v1.26.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions symfony/polyfill-php73 v1.26.0 v1.26.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions symfony/polyfill-php80 v1.26.0 v1.26.0 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions symfony/polyfill-php81 v1.26.0 v1.26.0 Symfony polyfill backporting some PHP 8.1+ features to lower PHP versions symfony/security-acl v3.3.2 v3.3.2 Symfony Security Component - ACL (Access Control List) symfony/string v5.4.14 v6.1.7 Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters i... ```

PHP version

$ php -v
PHP 8.1.10 (cli) (built: Sep  1 2022 21:43:31) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.10, Copyright (c), by Zend Technologies
    with Xdebug v3.1.5, Copyright (c) 2002-2022, by Derick Rethans

Subject

Steps to reproduce

Expected results

no errors

Actual results

Parameter $category of method ExampleMedia::setCategory() has invalid type Sonata\ClassificationBundle\Model\CategoryInterface.

This happened because I didn't install sonata-project/classification-bundle, because I don't need it. What to do?

dmitryuk commented 1 year ago

The same as own Admin (minified version):

class MediaAdmin extends BaseMediaAdmin
{
    public function __construct(
        Pool $pool,
        ?string $categoryManager = null,
        $contextManager = null
    ) {
        parent::__construct($pool, $categoryManager, $contextManager);
    }

Error:

Parameter $categoryManager of method CompanyMediaBundle\Admin\MediaAdmin::__construct() has invalid type  
         Sonata\ClassificationBundle\Model\CategoryManagerInterface. 
haivala commented 1 year ago

Impossible to help if we do not have your package information

dmitryuk commented 1 year ago

@haivala Thanks for your answer, I have updated packages info

VincentLanglet commented 1 year ago

@jordisala1991 did you encounter this issue ?

Isn't weird to have a lot of things typehinted or native typehinted with CategoryInterface or CategoryManagerInterface, without having the package in dependencies ? Not sure how to solve this, because requiring the categoryBundle is too much if people are not using it.

dmitryuk commented 1 year ago

What about to add interfaces to this package?

VincentLanglet commented 1 year ago

What about to add interfaces to this package?

https://github.com/sonata-project/SonataClassificationBundle/ can be use as a standalone package so need to have his own interfaces.

And I assume your application is working fine without installing sonata-project/classification-bundle, so it might be worth to report the issue to phpstan in order to see if there is a solution to avoid this error with the tool.