GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 7bf065...13ae28 )
by Axel
02:07
created

AttributeManager::retrieveClassicAttribute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 9
cts 10
cp 0.9
rs 9.2
cc 4
eloc 10
nc 4
nop 2
crap 4.016
1
<?php
2
3
namespace PhpAbac\Manager;
4
5
use PhpAbac\Model\AbstractAttribute;
6
use PhpAbac\Model\Attribute;
7
use PhpAbac\Model\EnvironmentAttribute;
8
9
class AttributeManager
10
{
11
    /** @var array **/
12
    private $attributes;
13
14
    /**
15
     * @param array $attributes
16
     */
17 25
    public function __construct($attributes)
18
    {
19 25
        $this->attributes = $attributes;
20 25
    }
21
22
    /**
23
     * @param string $attributeId
24
     * @return \PhpAbac\Model\AbstractAttribute
25
     */
26 8
    public function getAttribute($attributeId) {
27 8
        $attributeKeys = explode('.', $attributeId);
28
        // The first element will be the attribute ID, then the field ID
29 8
        $attributeId = array_shift($attributeKeys);
30 8
        $attributeName = implode('.', $attributeKeys);
31
        // The field ID is also the attribute object property
32 8
        $attributeData = $this->attributes[$attributeId];
33
        return
34 8
            ($attributeId === 'environment')
35 4
            ? $this->getEnvironmentAttribute($attributeData, $attributeName)
36 8
            : $this->getClassicAttribute($attributeData, $attributeName)
37
        ;
38
    }
39
40
    /**
41
     * @param array $attributeData
42
     * @param string $property
43
     * @return \PhpAbac\Model\Attribute
44
     */
45 6
    private function getClassicAttribute($attributeData, $property) {
46
        return
47 6
            (new Attribute())
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhpAbac\Model\AbstractAttribute as the method setProperty() does only exist in the following sub-classes of PhpAbac\Model\AbstractAttribute: PhpAbac\Model\Attribute. 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...
48 6
            ->setName($attributeData['fields'][$property]['name'])
49 6
            ->setType($attributeData['type'])
50 6
            ->setProperty($property)
51 6
            ->setSlug($this->slugify($attributeData['fields'][$property]['name']))
52
        ;
53
    }
54
55
    /**
56
     * @param array $attributeData
57
     * @param string $key
58
     * @return \PhpAbac\Model\EnvironmentAttribute
59
     */
60 4
    private function getEnvironmentAttribute($attributeData, $key) {
61
        return
62 4
            (new EnvironmentAttribute())
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhpAbac\Model\AbstractAttribute as the method setVariableName() does only exist in the following sub-classes of PhpAbac\Model\AbstractAttribute: PhpAbac\Model\EnvironmentAttribute. 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...
63 4
            ->setName($attributeData[$key]['name'])
64 4
            ->setType('environment')
65 4
            ->setVariableName($attributeData[$key]['variable_name'])
66 4
            ->setSlug($this->slugify($attributeData[$key]['name']))
67
        ;
68
    }
69
70
    /**
71
     * @param AbstractAttribute $attribute
72
     * @param string $attributeType
0 ignored issues
show
Documentation introduced by
There is no parameter named $attributeType. Did you maybe mean $attribute?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
73
     * @param object $user
74
     * @param object $object
75
     * @return mixed
76
     */
77 6
    public function retrieveAttribute(AbstractAttribute $attribute, $user = null, $object = null)
78
    {
79 6
        switch($attribute->getType()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after SWITCH keyword; 0 found
Loading history...
80 6
            case 'user':
81 3
                return $this->retrieveClassicAttribute($attribute, $user);
0 ignored issues
show
Compatibility introduced by
$attribute of type object<PhpAbac\Model\AbstractAttribute> is not a sub-type of object<PhpAbac\Model\Attribute>. It seems like you assume a child class of the class PhpAbac\Model\AbstractAttribute to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Bug introduced by
It seems like $user defined by parameter $user on line 77 can also be of type null; however, PhpAbac\Manager\Attribut...rieveClassicAttribute() does only seem to accept object, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
82 4
            case 'resource':
83 3
                return $this->retrieveClassicAttribute($attribute, $object);
0 ignored issues
show
Compatibility introduced by
$attribute of type object<PhpAbac\Model\AbstractAttribute> is not a sub-type of object<PhpAbac\Model\Attribute>. It seems like you assume a child class of the class PhpAbac\Model\AbstractAttribute to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Bug introduced by
It seems like $object defined by parameter $object on line 77 can also be of type null; however, PhpAbac\Manager\Attribut...rieveClassicAttribute() does only seem to accept object, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
84 2
            case 'environment':
85 2
                return $this->retrieveEnvironmentAttribute($attribute);
0 ignored issues
show
Compatibility introduced by
$attribute of type object<PhpAbac\Model\AbstractAttribute> is not a sub-type of object<PhpAbac\Model\EnvironmentAttribute>. It seems like you assume a child class of the class PhpAbac\Model\AbstractAttribute to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
86
        }
87
    }
88
89
    /**
90
     * @param Attribute $attribute
91
     * @param object $object
92
     * @return mixed
93
     */
94 5
    private function retrieveClassicAttribute(Attribute $attribute, $object)
95
    {
96 5
        $propertyPath = explode('.', $attribute->getProperty());
97 5
        $propertyValue = $object;
98 5
        foreach($propertyPath as $property) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
99 5
            $getter = 'get'.ucfirst($property);
100 5
            if(!method_exists($propertyValue, $getter)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
101
                throw new \InvalidArgumentException('There is no getter for the "'.$attribute->getProperty().'" attribute for object "'.get_class($propertyValue).'"');
102
            }
103 5
            if (($propertyValue = $propertyValue->{$getter}()) === null) {
104 5
                return null;
105
            }
106
        }
107 5
        return $propertyValue;
108
    }
109
110
    /**
111
     *
112
     * @param \PhpAbac\Model\EnvironmentAttribute $attribute
113
     * @return mixed
114
     */
115 2
    private function retrieveEnvironmentAttribute(EnvironmentAttribute $attribute) {
116 2
        return getenv($attribute->getVariableName());
117
    }
118
119
    /*
120
     * @param string $name
121
     * @return string
122
     */
123 8
    public function slugify($name)
124
    {
125
        // replace non letter or digits by -
126 8
        $name = trim(preg_replace('~[^\\pL\d]+~u', '-', $name), '-');
127
        // transliterate
128 8
        if (function_exists('iconv')) {
129 8
            $name = iconv('utf-8', 'us-ascii//TRANSLIT', $name);
130
        }
131
        // remove unwanted characters
132 8
        $name = preg_replace('~[^-\w]+~', '', strtolower($name));
133 8
        if (empty($name)) {
134
            return 'n-a';
135
        }
136 8
        return $name;
137
    }
138
}
139