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 ( 84c3a7...ff5597 )
by Stan
02:46
created

Property   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 7
Bugs 0 Features 3
Metric Value
wmc 23
c 7
b 0
f 3
lcom 1
cbo 2
dl 0
loc 140
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSetGetMethodName() 0 10 2
A setProperties() 0 6 2
A setProperty() 0 14 3
A getPropertiesAsString() 0 6 2
D getPropertiesArray() 0 35 9
A validateProperties() 0 8 4
A asJson() 0 6 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\Traits;
12
13
use Teebot\Exception\Output;
14
use Teebot\Exception\Critical;
15
16
trait Property
17
{
18
    /**
19
     * Returns camel cased property's getter or setter method name. Checks method for existence.
20
     *
21
     * @param string $prefix Prefix of the method e.g. "set" or "get"
22
     * @param string $name   Method's name
23
     *
24
     * @return null|string
25
     */
26
    protected function getSetGetMethodName($prefix, $name)
27
    {
28
        $setter = $prefix . str_replace("_", "", ucwords($name, "_"));
29
30
        if (method_exists($this, $setter)) {
31
            return $setter;
32
        }
33
34
        return null;
35
    }
36
37
    /**
38
     * Sets properties of the class from array.
39
     *
40
     * @param array $data An associative array with property => value data
41
     */
42
    protected function setProperties(array $data)
43
    {
44
        foreach ($data as $name => $value) {
45
            $this->setProperty($name, $value);
46
        }
47
    }
48
49
    /**
50
     * Sets property of the class.
51
     *
52
     * @param string     $name  Property name
53
     * @param null|mixed $value Value of the property
54
     */
55
    protected function setProperty($name, $value = null)
56
    {
57
        $setterMethod = $this->getSetGetMethodName("set", $name);
58
59
        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...
60
            $this->{$setterMethod}($value);
61
62
            return;
63
        }
64
65
        if (property_exists($this, $name)) {
66
            $this->{$name} = $value;
67
        }
68
    }
69
70
    /**
71
     * Returns properties as string. Used for building get query string if GET method was set.
72
     *
73
     * @return string
74
     */
75
    public function getPropertiesAsString()
76
    {
77
        $properties = $this->getPropertiesArray();
78
79
        return $properties ? http_build_query($properties) : '';
80
    }
81
82
    /**
83
     * Returns an array with properties. Array with supported properties should be defined
84
     * in the class.
85
     *
86
     * @param bool $validate Flag whether validation for required properties should be applied
87
     *
88
     * @return array
89
     */
90
    public function getPropertiesArray($validate = true)
91
    {
92
        $properties = [];
93
94
        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...
95
            return $properties;
96
        }
97
98
        foreach ($this->supportedProperties as $name => $isRequired) {
99
100
            $getterMethod = $this->getSetGetMethodName("get", $name);
101
102
            if ($getterMethod && $this->{$getterMethod}() !== null) {
103
                $properties[$name] = $this->{$getterMethod}();
104
105
                continue;
106
            }
107
108
            if (property_exists($this, $name) && $this->{$name} !== null) {
109
                $properties[$name] = $this->{$name};
110
            }
111
        }
112
113
        if ($validate) {
114
            try {
115
                $this->validateProperties($properties);
116
            } catch (Critical $e) {
117
                Output::log($e);
118
119
                $properties = [];
120
            }
121
        }
122
123
        return $properties;
124
    }
125
126
    /**
127
     * Validates properties and checks which are required
128
     *
129
     * @param array $properties An associative array of the properties
130
     *
131
     * @throws Critical
132
     */
133
    protected function validateProperties($properties)
134
    {
135
        foreach ($this->supportedProperties as $propertyName => $isRequired) {
136
            if ($isRequired === true && empty($properties[$propertyName])) {
137
                throw new Critical('Required property "'.$propertyName.'" is not set!');
138
            }
139
        }
140
    }
141
142
    /**
143
     * Returns object's properties encoded as JSON string
144
     *
145
     * @param bool $validate Flag whether validation for required properties should be applied
146
     *
147
     * @return string
148
     */
149
    public function asJson($validate = true)
150
    {
151
        $properties = $this->getPropertiesArray($validate);
152
153
        return json_encode($properties);
154
    }
155
}
156