Total Complexity | 47 |
Total Lines | 178 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Complex classes like Operator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Operator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Operator extends TightModel implements OperatorInterface |
||
10 | { |
||
11 | public const TABLE_NAME = 'kadro_operator'; |
||
12 | public const TABLE_ALIAS = 'operator'; |
||
13 | |||
14 | |||
15 | protected $permissions = null; |
||
16 | |||
17 | // use Permissionability; |
||
18 | use \HexMakina\TightORM\RelationManyToMany; |
||
19 | |||
20 | public function __toString() |
||
21 | { |
||
22 | return $this->get('username'); |
||
23 | } |
||
24 | |||
25 | public function isActive(): bool |
||
26 | { |
||
27 | return !empty($this->get('active')); |
||
28 | } |
||
29 | |||
30 | public function operatorId() |
||
31 | { |
||
32 | return $this->getId(); |
||
33 | } |
||
34 | |||
35 | public function username() |
||
36 | { |
||
37 | return $this->get('username'); |
||
38 | } |
||
39 | |||
40 | // TODO remove this, pwd only useful when checkinin |
||
41 | public function password() |
||
42 | { |
||
43 | return $this->get('password'); |
||
44 | } |
||
45 | |||
46 | public function passwordChange($string) |
||
47 | { |
||
48 | $this->set('password', password_hash($this->validate_password($string), PASSWORD_DEFAULT)); |
||
49 | } |
||
50 | |||
51 | public function passwordVerify($string): bool |
||
52 | { |
||
53 | return password_verify($this->validate_password($string), $this->password() ?? ''); |
||
54 | } |
||
55 | |||
56 | public function name() |
||
57 | { |
||
58 | return $this->get('name'); |
||
59 | } |
||
60 | |||
61 | public function email() |
||
62 | { |
||
63 | return $this->get('email'); |
||
64 | } |
||
65 | public function phone() |
||
66 | { |
||
67 | return $this->get('phone'); |
||
68 | } |
||
69 | |||
70 | public function languageCode() |
||
71 | { |
||
72 | return $this->get('language_code'); |
||
73 | } |
||
74 | |||
75 | public static function safeLoading($op_id): OperatorInterface |
||
76 | { |
||
77 | $op = static::one($op_id); |
||
78 | $op->set('password', null); |
||
79 | return $op; |
||
80 | } |
||
81 | |||
82 | public static function query_retrieve($filters = [], $options = []): SelectInterface |
||
83 | { |
||
84 | $Query = static::table()->select(); |
||
85 | if (isset($options['eager']) && $options['eager'] === true) { |
||
86 | $Query->groupBy('id'); |
||
87 | |||
88 | $Query->autoJoin([ACL::table(), 'acl'], null, 'LEFT OUTER'); |
||
|
|||
89 | $Query->autoJoin([Permission::table(), 'kadro_permission'], null, 'LEFT OUTER'); |
||
90 | $Query->selectAlso(["GROUP_CONCAT(DISTINCT kadro_permission.id) as permission_ids", "GROUP_CONCAT(DISTINCT kadro_permission.name) as permission_names"]); |
||
91 | } |
||
92 | |||
93 | if (isset($filters['model']) && !empty($filters['model'])) { |
||
94 | $Query->join([static::otm('t'), static::otm('a')], [[static::otm('a'),static::otm('k'), 't_from','id']], 'INNER'); |
||
95 | $Query->whereFieldsEQ(['model_id' => $filters['model']->getId(), 'model_type' => get_class($filters['model'])::model_type()], static::otm('a')); |
||
96 | } |
||
97 | |||
98 | $Query->orderBy([$Query->tableLabel(), 'name', 'ASC']); |
||
99 | |||
100 | |||
101 | return $Query; |
||
102 | } |
||
103 | |||
104 | public function immortal(): bool |
||
105 | { |
||
106 | return true; // never delete a user, always deactivate |
||
107 | } |
||
108 | |||
109 | public function permission_names() |
||
123 | } |
||
124 | } |
||
125 | |||
126 | public function permissions() |
||
127 | { |
||
128 | |||
144 | } |
||
145 | |||
146 | public function hasPermission($p): bool |
||
147 | { |
||
148 | // new instances or inactive operators, none shall pass |
||
149 | if ($this->isNew() === true || $this->isActive() === false) { |
||
150 | return false; |
||
151 | } |
||
152 | |||
153 | $permission_name = $permission_id = null; |
||
154 | if (is_subclass_of($p, '\HexMakina\kadro\Auth\Permission')) { |
||
155 | $permission_name = $p->get('name'); |
||
156 | $permission_id = $p->getId(); |
||
157 | } elseif (preg_match('/[0-9]+/', $p)) { |
||
158 | $permission_id = $p; |
||
159 | } else { |
||
160 | $permission_name = $p; |
||
161 | } |
||
162 | |||
163 | if (!is_null($this->get('permission_names')) && !is_null($permission_name)) { |
||
164 | return strpos($this->get('permission_names'), $permission_name) !== false; |
||
165 | } elseif (!is_null($this->get('permission_ids')) && !is_null($permission_id)) { |
||
166 | return strpos($this->get('permission_ids'), $permission_id) !== false; |
||
167 | } elseif (!is_null($permission_name)) { |
||
168 | if (method_exists($this, $permission_name) && $this->$permission_name() == true) { |
||
169 | return true; |
||
170 | } elseif (property_exists($this, $permission_name) && $this->get('$permission_name') == true) { |
||
171 | return true; |
||
172 | } elseif (ACL::match($this, $permission_name) === true) { |
||
173 | return true; |
||
174 | } |
||
175 | } |
||
176 | |||
177 | return false; |
||
178 | } |
||
179 | |||
180 | private function validate_password($string): string |
||
187 | } |
||
188 | } |
||
189 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.