magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.47k stars 9.28k forks source link

PHPStan returns false positives because of incoherent PHPDocs on functions/parameters that accept Phrase objects #36063

Open guvra opened 2 years ago

guvra commented 2 years ago

Preconditions and environment

Steps to reproduce

  1. At the root of your Magento project, create a file named Test.php with the following contents:
    
    <?php

declare(strict_types=1);

namespace Foo\Bar\Controller;

use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\HttpGetActionInterface; use Magento\Framework\View\Result\Page;

class Test extends Action implements HttpGetActionInterface { public function execute() { $this->messageManager->addErrorMessage(__('foobar'));

    return $this->resultFactory->create(Page::class);
}

}

2. Run PHPStan:

vendor/bin/phpstan analyse --level 6 -c dev/tests/static/testsuite/Magento/Test/Php/_files/phpstan/phpstan.neon Test.php


### Expected result

PHPStan shouldn't report any error.

### Actual result

PHPStan reports the following error:

Parameter #1 $message of method Magento\Framework\Message\ManagerInterface::addErrorMessage() expects string, Magento\Framework\Phrase given.


### Additional information

To fix this issue, the typehints of parameters and methods that accept Phrase objects should be changed from `string` to `string|Phrase`.

A few examples:

In Magento\Backend\Block\Widget\Container:

```php
    /**
     * @var string
     */
    protected $_headerText = 'Container Widget Header';

Must be changed to:

    /**
     * @var string|Phrase
     */
    protected $_headerText = 'Container Widget Header';

In Magento\Backend\Block\Widget\Button\ButtonList:

    /**
     * Update specified button property
     *
     * @param string $buttonId
     * @param string|null $key
     * @param string $data
     * @return void
     */
    public function update($buttonId, $key, $data)
    {

Must be changed to:

    /**
     * Update specified button property
     *
     * @param string $buttonId
     * @param string|null $key
     * @param string|Phrase $data
     * @return void
     */
    public function update($buttonId, $key, $data)
    {

In Magento\Framework\Message\ManagerInterface:

    /**
     * Adds new error message
     *
     * @param string $message
     * @param string|null $group
     * @return ManagerInterface
     */
    public function addErrorMessage($message, $group = null);

Must be changed to:

    /**
     * Adds new error message
     *
     * @param string|Phrase $message
     * @param string|null $group
     * @return ManagerInterface
     */
    public function addErrorMessage($message, $group = null);

(same for the other addXxX methods of the same class).

In Magento\Framework\View\Page\Title:

    /**
     * Set page title
     *
     * @param string $title
     * @return $this
     */
    public function set($title)
    {

Must be changed to:

    /**
     * Set page title
     *
     * @param string|Phrase $title
     * @return $this
     */
    public function set($title)
    {

Other option: add Stringable interface to the phrase class and use string|Stringable.

Release note

No response

Triage and priority

m2-assistant[bot] commented 2 years ago

Hi @guvra. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

guvra commented 2 years ago

Until it is fixed, I implemented a workaround with a phpstan extension:

<?php

declare(strict_types=1);

namespace Magento\PhpStan\Type\Php;

use Magento\Framework\Phrase;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;

class TranslationFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
    public function isFunctionSupported(FunctionReflection $functionReflection): bool
    {
        return $functionReflection->getName() === '__';
    }

    public function getTypeFromFunctionCall(
        FunctionReflection $functionReflection,
        FuncCall $functionCall,
        Scope $scope
    ): ?Type {
        return new UnionType([new StringType(), new ObjectType(Phrase::class)]);
    }
}

This workaround is not ideal, because this extension will prevent phpstan from displaying an error when a Phrase object is passed to a function that only accepts strings (e.g. public function foo(string $text).

m2-assistant[bot] commented 1 year ago

Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Hotel commented 1 year ago

Hello @guvra,

Thanks for the report and collaboration!

We have tried to reproduce the issue in Magento 2.4-develop instance and the issue is reproducible for us with the same steps as mentioned in the main description:

  1. At the root of your Magento project, create a file named Test.php with the following contents:
    
    <?php

declare(strict_types=1);

namespace Foo\Bar\Controller;

use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\HttpGetActionInterface; use Magento\Framework\View\Result\Page;

class Test extends Action implements HttpGetActionInterface { public function execute() { $this->messageManager->addErrorMessage(__('foobar'));

    return $this->resultFactory->create(Page::class);
}

}

2. Run PHPStan:

vendor/bin/phpstan analyse --level 6 -c dev/tests/static/testsuite/Magento/Test/Php/_files/phpstan/phpstan.neon Test.php



Please find below the screenshot of the error for reference:

<img width="1403" alt="image" src="https://user-images.githubusercontent.com/51681618/192763551-5b953ffa-628f-45a5-b58d-7b5ac201ac28.png">

Hence confirming the issue.

Thanks
github-jira-sync-bot commented 1 year ago

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-6760 is successfully created for this GitHub issue.

m2-assistant[bot] commented 1 year ago

:white_check_mark: Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

antoniocarboni commented 4 months ago

I temp. fix with this row on ignoreErrors on phpstan config file

'#Parameter \#1 \$data of method Magento\\Framework\\Escaper::escapeHtml\(\) expects array\|string, Magento\\Framework\\Phrase given.#'