sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Incorrect typehint for AdminInterface::toString() introduced in 3.76.0 #6403

Closed jaikdean closed 3 years ago

jaikdean commented 4 years ago

Environment

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.76.0 3.76.0 The missing Symfony Admin Generator
sonata-project/block-bundle              4.3.0  4.3.0  Symfony SonataBlockBundle
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/doctrine-extensions       1.9.1  1.9.1  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 3.23.0 3.23.0 Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  2.3.0  2.3.0  Lightweight Exporter library
sonata-project/form-extensions           1.6.0  1.6.0  Symfony form extensions
sonata-project/twig-extensions           1.4.1  1.4.1  Sonata twig extensions

Symfony packages

$ composer show --latest 'symfony/*'
symfony/asset                      v5.1.5  v5.1.5  Symfony Asset Component
symfony/browser-kit                v5.1.5  v5.1.5  Symfony BrowserKit Component
symfony/cache                      v5.1.5  v5.1.5  Symfony Cache component with PSR-6, PSR-16, and tags
symfony/cache-contracts            v2.1.3  v2.2.0  Generic abstractions related to caching
symfony/config                     v4.4.13 v5.1.5  Symfony Config Component
symfony/console                    v4.4.13 v5.1.5  Symfony Console Component
symfony/css-selector               v5.1.5  v5.1.5  Symfony CssSelector Component
symfony/debug                      v4.4.13 v4.4.13 Symfony Debug Component
symfony/dependency-injection       v4.4.13 v5.1.5  Symfony DependencyInjection Component
symfony/deprecation-contracts      v2.1.3  v2.2.0  A generic function and convention to trigger deprecation notices
symfony/doctrine-bridge            v4.4.13 v5.1.5  Symfony Doctrine Bridge
symfony/dom-crawler                v5.1.5  v5.1.5  Symfony DomCrawler Component
symfony/dotenv                     v5.1.5  v5.1.5  Registers environment variables from a .env file
symfony/error-handler              v4.4.13 v5.1.5  Symfony ErrorHandler Component
symfony/event-dispatcher           v4.4.13 v5.1.5  Symfony EventDispatcher Component
symfony/event-dispatcher-contracts v1.1.9  v2.2.0  Generic abstractions related to dispatching event
symfony/expression-language        v5.1.5  v5.1.5  Symfony ExpressionLanguage Component
symfony/filesystem                 v5.1.5  v5.1.5  Symfony Filesystem Component
symfony/finder                     v5.1.5  v5.1.5  Symfony Finder Component
symfony/flex                       v1.9.3  v1.9.4  Composer plugin for Symfony
symfony/form                       v4.4.13 v5.1.5  Symfony Form Component
symfony/framework-bundle           v4.4.13 v5.1.5  Symfony FrameworkBundle
symfony/http-foundation            v4.4.13 v5.1.5  Symfony HttpFoundation Component
symfony/http-kernel                v4.4.13 v5.1.5  Symfony HttpKernel Component
symfony/intl                       v5.1.5  v5.1.5  A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/lock                       v5.1.5  v5.1.5  Symfony Lock Component
symfony/mime                       v5.1.5  v5.1.5  A library to manipulate MIME messages
symfony/monolog-bridge             v5.1.5  v5.1.5  Symfony Monolog Bridge
symfony/monolog-bundle             v3.5.0  v3.5.0  Symfony MonologBundle
symfony/options-resolver           v5.1.5  v5.1.5  Symfony OptionsResolver Component
symfony/phpunit-bridge             v5.1.5  v5.1.5  Symfony PHPUnit Bridge
symfony/polyfill-ctype             v1.18.1 v1.18.1 Symfony polyfill for ctype functions
symfony/polyfill-intl-grapheme     v1.18.1 v1.18.1 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-idn          v1.18.1 v1.18.1 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer   v1.18.1 v1.18.1 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-php80             v1.18.1 v1.18.1 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/process                    v5.1.5  v5.1.5  Symfony Process Component
symfony/property-access            v5.1.5  v5.1.5  Symfony PropertyAccess Component
symfony/property-info              v5.1.5  v5.1.5  Symfony Property Info Component
symfony/proxy-manager-bridge       v4.4.13 v5.1.5  Symfony ProxyManager Bridge
symfony/psr-http-message-bridge    v2.0.1  v2.0.1  PSR HTTP message bridge
symfony/routing                    v4.4.13 v5.1.5  Symfony Routing Component
symfony/security-acl               v3.1.0  v3.1.0  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle            v4.4.13 v5.1.5  Symfony SecurityBundle
symfony/security-core              v4.4.13 v5.1.5  Symfony Security Component - Core Library
symfony/security-csrf              v4.4.13 v5.1.5  Symfony Security Component - CSRF Library
symfony/security-guard             v4.4.13 v5.1.5  Symfony Security Component - Guard
symfony/security-http              v4.4.13 v5.1.5  Symfony Security Component - HTTP Integration
symfony/serializer                 v5.1.5  v5.1.5  Symfony Serializer Component
symfony/service-contracts          v2.1.3  v2.2.0  Generic abstractions related to writing services
symfony/stopwatch                  v5.1.5  v5.1.5  Symfony Stopwatch Component
symfony/string                     v5.1.5  v5.1.5  Symfony String component
symfony/swiftmailer-bundle         v3.4.0  v3.4.0  Symfony SwiftmailerBundle
symfony/templating                 v5.1.5  v5.1.5  Symfony Templating Component
symfony/test-pack                  v1.0.6  v1.0.6  A pack for functional and end-to-end testing within a Symfony app
symfony/translation                v4.4.13 v5.1.5  Symfony Translation Component
symfony/translation-contracts      v2.1.3  v2.2.0  Generic abstractions related to translation
symfony/twig-bridge                v4.4.13 v5.1.5  Symfony Twig Bridge
symfony/twig-bundle                v4.4.13 v5.1.5  Symfony TwigBundle
symfony/validator                  v4.4.13 v5.1.5  Symfony Validator Component
symfony/var-dumper                 v5.1.5  v5.1.5  Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter               v5.1.5  v5.1.5  A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-profiler-bundle        v5.0.11 v5.1.5  Symfony WebProfilerBundle
symfony/workflow                   v5.1.5  v5.1.5  Symfony Workflow Component
symfony/yaml                       v5.1.5  v5.1.5  Symfony Yaml Component

PHP version

$ php -v
PHP 7.4.10 (cli) (built: Sep  3 2020 18:21:42) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans
    with Zend OPcache v7.4.10, Copyright (c), by Zend Technologies

Subject

3.76.0 introduced a number of typehints in annotations. The argument for AdminInterface::toString($object) is typehinted as object, but is sometimes passed null. For example, BreadcrumbsBuilder passes the returned value from AdminInterface::getSubject(), which is typehinted as object|null.

VincentLanglet commented 4 years ago

The typehint is correct. toString expects to be only used with object. It currently trigger a deprecation, but will throw an exception in 4.0.

In the example you get, there is a check $admin->hasSubject(), so the subject can't be null.

Any other example ?

jaikdean commented 4 years ago

The static analysis isn't configured to know that hasSubject() returning true means that getSubject() will return object, as things could change inbetween the calls. We use Psalm on our projects rather than PHPStan, but it interprets the PHPStan typehints and rightly complains about this. For example the following code results in null being passed to toString():

if ($admin->hasSubject()) {
    $admin->setSubject(null);
    $admin->toString($admin->getSubject());
}

The AbstractAdmin::toString() implementation still accepts null as an argument. It throws a deprecation warning, but passing null is still allowed by this method in 3.x.

GetShortObjectDescriptionAction::__invoke() has a code path that results in calling toString(null) for JSON responses.

VincentLanglet commented 4 years ago

The static analysis isn't configured to know that hasSubject() returning true means that getSubject() will return object, as things could change inbetween the calls. We use Psalm on our projects rather than PHPStan, but it interprets the PHPStan typehints and rightly complains about this. For example the following code results in null being passed to toString():

if ($admin->hasSubject()) {
    $admin->setSubject(null);
    $admin->toString($admin->getSubject());
}

$admin->getSubject() will return object in 4.0 and throw an exception if there is no object, so you should rely on hasSubject.

GetShortObjectDescriptionAction::__invoke() has a code path that results in calling toString(null) for JSON responses.

As explained in the comment of this method, in next major it will return a NotFound exception.

throw new NotFoundHttpException(sprintf('Could not find subject for id "%s"', $objectId));

I still see no issue with the toString method.

jaikdean commented 4 years ago

I understand the deprecations and upcoming BC-breaks, but these typehints are in 3.x releases. The typehint for toString() in 3.x says it only accepts object, but that's not true. The deprecation warnings should indicate upcoming deprecations, the typehints should document what the methods currently accept, not recommendations for the future.

Perhaps it's a difference between how PHPStan and Psalm work, but in Psalm the typehint annotations define the interface for the method, they're not just suggestions. After updating our project from 3.75.0 to 3.76.0, we got a huge pile of type errors because of these changes.

VincentLanglet commented 4 years ago

I understand the deprecations and upcoming BC-breaks, but these typehints are in 3.x releases. The typehint for toString() in 3.x says it only accepts object, but that's not true. The deprecation warnings should indicate upcoming deprecations, the typehints should document what the methods currently accept, not recommendations for the future.

If you really document what the methods currently accept, you should use mixed, since we're returning '' value then. But that doesn't help the user to correctly use this function.

If I have a function

/**
 * @param int $int
 */
function addOne($int) {
   return $int + 1;
}

You could say in a similar way that int is not a correct typehint, since it can work with others value.

Perhaps it's a difference between how PHPStan and Psalm work, but in Psalm the typehint annotations define the interface for the method, they're not just suggestions. After updating our project from 3.75.0 to 3.76.0, we got a huge pile of type errors because of these changes.

Currently you talked about BreadcrumbsBuilder and GetShortObjectDescriptionAction. If there are some Psalm errors, they should not coming from Sonata code, but from yours. Do you have any example ?

For instance,

if ($admin->hasSubject()) {
    \assert(null !== $admin->getSubject());
    $admin->toString($admin->getSubject());
}

is one way to fix them.

I also had multiple error from Phpstan when updating the Sonata version, but it was coming from the introduction of generics.