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 ( 5ac723...c719eb )
by Stan
07:02
created

Property::getPropertiesArray()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 14
nc 9
nop 1
1
<?php
2
3
/**
4
 * Trait with methods to work with properties. Used in Method and Entity classes.
5
 *
6
 * @package Teebot (Telegram bot framework)
7
 *
8
 * @author  Stanislav Drozdov <[email protected]>
9
 */
10
11
namespace Teebot\Api\Traits;
12
13
use Teebot\Api\Exception\PropertyException;
14
15
trait Property
16
{
17
    /**
18
     * Returns camel cased property's getter or setter method name. Checks method for existence.
19
     *
20
     * @param string $prefix Prefix of the method e.g. "set" or "get"
21
     * @param string $name   Method's name
22
     *
23
     * @return null|string
24
     */
25
    protected function getSetGetMethodName($prefix, $name)
26
    {
27
        $setter = $prefix . str_replace("_", "", ucwords($name, "_"));
28
29
        if (method_exists($this, $setter)) {
30
            return $setter;
31
        }
32
33
        return null;
34
    }
35
36
    /**
37
     * Sets properties of the class from array.
38
     *
39
     * @param array $data An associative array with property => value data
40
     */
41
    protected function setProperties(array $data)
42
    {
43
        foreach ($data as $name => $value) {
44
            $this->setProperty($name, $value);
45
        }
46
    }
47
48
    /**
49
     * Sets property of the class.
50
     *
51
     * @param string     $name  Property name
52
     * @param null|mixed $value Value of the property
53
     */
54
    protected function setProperty($name, $value = null)
55
    {
56
        $setterMethod = $this->getSetGetMethodName("set", $name);
57
58
        if ($setterMethod) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $setterMethod of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
59
            $this->{$setterMethod}($value);
60
61
            return;
62
        }
63
64
        if (property_exists($this, $name)) {
65
            $this->{$name} = $value;
66
        }
67
    }
68
69
    /**
70
     * Returns properties as string. Used for building get query string if GET method was set.
71
     *
72
     * @return string
73
     */
74
    public function getPropertiesAsString()
75
    {
76
        $properties = $this->getPropertiesArray();
77
78
        return $properties ? http_build_query($properties) : '';
79
    }
80
81
    /**
82
     * Returns an array with properties. Array with supported properties should be defined
83
     * in the class.
84
     *
85
     * @param bool $validate Flag whether validation for required properties should be applied
86
     *
87
     * @return array
88
     */
89
    public function getPropertiesArray($validate = true)
90
    {
91
        $properties = [];
92
93
        if (empty($this->supportedProperties)) {
0 ignored issues
show
Bug introduced by
The property supportedProperties does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
94
            return $properties;
95
        }
96
97
        foreach ($this->supportedProperties as $name => $isRequired) {
98
99
            $getterMethod = $this->getSetGetMethodName("get", $name);
100
101
            if ($getterMethod && $this->{$getterMethod}() !== null) {
102
                $properties[$name] = $this->{$getterMethod}();
103
104
                continue;
105
            }
106
107
            if (property_exists($this, $name) && $this->{$name} !== null) {
108
                $properties[$name] = $this->{$name};
109
            }
110
        }
111
112
        if ($validate) {
113
            $this->validateProperties($properties);
114
        }
115
116
        return $properties;
117
    }
118
119
    public function getPropertiesMultipart($validate = true)
120
    {
121
        $requestProperties = [];
122
        $properties        = $this->getPropertiesArray($validate);
123
124
        foreach ($properties as $k => $v) {
125
            $requestProperties[] = [
126
                'name'     => $k,
127
                'contents' => $v,
128
            ];
129
        }
130
131
        return $requestProperties;
132
    }
133
134
    /**
135
     * Validates properties and checks which are required
136
     *
137
     * @param array $properties An associative array of the properties
138
     *
139
     * @throws PropertyException
140
     */
141
    protected function validateProperties($properties)
142
    {
143
        foreach ($this->supportedProperties as $propertyName => $isRequired) {
144
            if ($isRequired === true && empty($properties[$propertyName])) {
145
                throw new PropertyException('Required property "'.$propertyName.'" is not set!');
146
            }
147
        }
148
    }
149
150
    /**
151
     * Returns object's properties encoded as JSON string
152
     *
153
     * @param bool $validate Flag whether validation for required properties should be applied
154
     *
155
     * @return string
156
     */
157
    public function asJson($validate = true)
158
    {
159
        $properties = $this->getPropertiesArray($validate);
160
161
        return json_encode($properties);
162
    }
163
}
164