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.

AttributeManager::getAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
crap 2
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
    /** @var string Prefix to add before getter name (default)'get' */
15
    private $getter_prefix = 'get';
16
    /** @var string Function to apply on the getter name ( before adding prefix ) (default)'ucfirst' */
17
    private $getter_name_transformation_function = 'ucfirst';
18
    
19
20
    /**
21
     * @param array $attributes
22
	 * @param array $options           A List of option to configure This Abac Instance
23
	 *                                 Options list :
24
	 *                                 'getter_prefix' => Prefix to add before getter name (default)'get'
25
	 *                                 'getter_name_transformation_function' => Function to apply on the getter name ( before adding prefix ) (default)'ucfirst'
26
     */
27 28
    public function __construct($attributes, $options = [])
28
    {
29 28
        $this->attributes = $attributes;
30
	
31 28
		$options = array_intersect_key( $options, array_flip( [
32 28
			'getter_prefix',
33 28
			'getter_name_transformation_function',
34 28
		] ) );
35
		
36 28
		foreach($options as $name => $value) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
37
			$this->$name = $value;
38 28
		}
39 28
    }
40
41
    /**
42
     * @param string $attributeId
43
     * @return \PhpAbac\Model\AbstractAttribute
44
     */
45 11
    public function getAttribute($attributeId) {
46 11
        $attributeKeys = explode('.', $attributeId);
47
        // The first element will be the attribute ID, then the field ID
48 11
        $attributeId = array_shift($attributeKeys);
49 11
        $attributeName = implode('.', $attributeKeys);
50
        // The field ID is also the attribute object property
51 11
        $attributeData = $this->attributes[$attributeId];
52
        return
53 11
            ($attributeId === 'environment')
54 11
            ? $this->getEnvironmentAttribute($attributeData, $attributeName)
55 11
            : $this->getClassicAttribute($attributeData, $attributeName)
56 11
        ;
57
    }
58
59
    /**
60
     * @param array $attributeData
61
     * @param string $property
62
     * @return \PhpAbac\Model\Attribute
63
     */
64 9
    private function getClassicAttribute($attributeData, $property) {
65
        return
66 9
            (new Attribute())
67 9
            ->setName($attributeData['fields'][$property]['name'])
68 9
            ->setType($attributeData['type'])
69 9
            ->setProperty($property)
70 9
            ->setSlug($this->slugify($attributeData['fields'][$property]['name']))
71 9
        ;
72
    }
73
74
    /**
75
     * @param array $attributeData
76
     * @param string $key
77
     * @return \PhpAbac\Model\EnvironmentAttribute
78
     */
79 4
    private function getEnvironmentAttribute($attributeData, $key) {
80
        return
81 4
            (new EnvironmentAttribute())
82 4
            ->setName($attributeData[$key]['name'])
83 4
            ->setType('environment')
84 4
            ->setVariableName($attributeData[$key]['variable_name'])
85 4
            ->setSlug($this->slugify($attributeData[$key]['name']))
86 4
        ;
87
    }
88
89
    /**
90
     * @param AbstractAttribute $attribute
91
     * @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...
92
     * @param object $user
93
     * @param object $object
94
     * @return mixed
95
     */
96 9
    public function retrieveAttribute(AbstractAttribute $attribute, $user = null, $object = null, $getter_params = [])
97
    {
98 9
        switch($attribute->getType()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after SWITCH keyword; 0 found
Loading history...
99 9
            case 'user':
100 6
                return $this->retrieveClassicAttribute($attribute, $user, $getter_params);
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 96 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...
101 6
            case 'resource':
102 5
                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 96 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...
103 2
            case 'environment':
104 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...
105
        }
106
    }
107
108
    /**
109
     * @param Attribute $attribute
110
     * @param object $object
111
     * @return mixed
112
     */
113 8
    private function retrieveClassicAttribute(Attribute $attribute, $object, $getter_params = [])
114
    {
115 8
        $propertyPath = explode('.', $attribute->getProperty());
116 8
        $propertyValue = $object;
117 8
        foreach($propertyPath as $property) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
118
	
119
        	
120 8
			$getter = $this->getter_prefix.call_user_func($this->getter_name_transformation_function,$property);
121
            // Use is_callable, instead of method_exists, to deal with __call magic method
122 8
            if(!is_callable([$propertyValue,$getter])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
123
                throw new \InvalidArgumentException('There is no getter for the "'.$attribute->getProperty().'" attribute for object "'.get_class($propertyValue).'" with getter "'.$getter.'"');
124
            }
125 8
			if ( ( $propertyValue = call_user_func_array( [
126 8
					$propertyValue,
127 8
					$getter,
128 8
				], isset( $getter_params[ $property ] ) ? $getter_params[ $property ] : [] ) ) === null
129 8
			) {
130 1
				return null;
131
			}
132 8
        }
133 8
        return $propertyValue;
134
    }
135
136
    /**
137
     *
138
     * @param \PhpAbac\Model\EnvironmentAttribute $attribute
139
     * @return mixed
140
     */
141 2
    private function retrieveEnvironmentAttribute(EnvironmentAttribute $attribute) {
142 2
        return getenv($attribute->getVariableName());
143
    }
144
145
    /*
146
     * @param string $name
147
     * @return string
148
     */
149 11
    public function slugify($name)
150
    {
151
        // replace non letter or digits by -
152 11
        $name = trim(preg_replace('~[^\\pL\d]+~u', '-', $name), '-');
153
        // transliterate
154 11
        if (function_exists('iconv')) {
155 11
            $name = iconv('utf-8', 'us-ascii//TRANSLIT', $name);
156 11
        }
157
        // remove unwanted characters
158 11
        $name = preg_replace('~[^-\w]+~', '', strtolower($name));
159 11
        if (empty($name)) {
160
            return 'n-a';
161
        }
162 11
        return $name;
163
    }
164
}
165