I18nResponseErrorFormatter::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
4
namespace hipanel\components;
5
6
7
use hiqdev\hiart\Exception;
8
use Yii;
9
10
/**
11
 * Class I18nResponseErrorFormatter
12
 * @package hipanel\components
13
 */
14
final class I18nResponseErrorFormatter
15
{
16
    /**
17
     * @var string
18
     */
19
    private $dictionary;
20
21
    /**
22
     * I18nResponseErrorFormatter constructor.
23
     * @param string $dictionary
24
     */
25
    public function __construct(string $dictionary = 'hipanel')
26
    {
27
        $this->dictionary = $dictionary;
28
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function __invoke(Exception $exception): string
34
    {
35
        $responseData = $exception->getResponseData();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class hiqdev\hiart\Exception as the method getResponseData() does only exist in the following sub-classes of hiqdev\hiart\Exception: hiqdev\hiart\ResponseDecodingException, hiqdev\hiart\ResponseErrorException. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
36
        $message = $exception->getMessage();
37
        if (!empty($responseData['_error_ops'])) {
38
            $message = Yii::t($this->dictionary, $message, $responseData['_error_ops']);
39
        } else {
40
            $message = Yii::t($this->dictionary, $message);
41
        }
42
        return $message;
43
    }
44
}
45