Passed
Push — master ( 477ee0...c2086c )
by Eugene
03:26
created

ActionList   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 5
c 3
b 0
f 2
lcom 0
cbo 1
dl 0
loc 38
ccs 0
cts 15
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A allowedProperties() 0 7 1
A transformTokenToRoute() 0 13 3
1
<?php
2
namespace App\Actions\Image;
3
4
use League\Flysystem\FilesystemInterface;
5
use Staticus\Middlewares\ActionListAbstract;
6
use Staticus\Resources\Image\ResourceImageDO;
7
use Staticus\Resources\Image\ResourceImageDOInterface;
8
9
class ActionList extends ActionListAbstract
10
{
11
    public function __construct(
12
        ResourceImageDOInterface $resourceDO
13
        , FilesystemInterface $filesystem
14
    )
15
    {
16
        parent::__construct($resourceDO, $filesystem);
17
    }
18
19
    protected function allowedProperties()
20
    {
21
        $allowed = parent::allowedProperties();
22
        $allowed[] = ResourceImageDO::TOKEN_DIMENSION;
23
24
        return $allowed;
25
    }
26
27
    /**
28
     * @param string $token
29
     * @param string $value
30
     * @param array $query
31
     * @return string
32
     */
33
    protected function transformTokenToRoute($token, $value, array $query)
34
    {
35
        $query = parent::transformTokenToRoute($token, $value, $query);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Staticus\Middlewares\ActionListAbstract as the method transformTokenToRoute() does only exist in the following sub-classes of Staticus\Middlewares\ActionListAbstract: App\Actions\Image\ActionList. 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
        switch ($token) {
37
            case ResourceImageDO::TOKEN_DIMENSION:
38
                if ((int)$value !== ResourceImageDO::DEFAULT_DIMENSION) {
39
                    $query['size'] = $value;
40
                }
41
                break;
42
        }
43
44
        return $query;
45
    }
46
}