Total Complexity | 58 |
Total Lines | 179 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like AbstractProcessor 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 AbstractProcessor, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
42 | abstract class AbstractProcessor implements ProcessorInterface |
||
43 | { |
||
44 | public AbstractEntity $Entity; |
||
45 | |||
46 | public string $content = ''; |
||
47 | |||
48 | public string $target = ''; |
||
49 | |||
50 | protected string $action; |
||
51 | |||
52 | protected ?int $id = null; |
||
53 | |||
54 | protected CrudInterface | Users $Model; |
||
55 | |||
56 | protected array $extra = array(); |
||
57 | |||
58 | public function __construct(private Users $Users, Request $request) |
||
59 | { |
||
60 | $this->process($request); |
||
61 | } |
||
62 | |||
63 | public function getModel(): CrudInterface | Users |
||
64 | { |
||
65 | return $this->Model; |
||
66 | } |
||
67 | |||
68 | public function getAction(): string |
||
69 | { |
||
70 | return $this->action; |
||
71 | } |
||
72 | |||
73 | public function getTarget(): string |
||
74 | { |
||
75 | return $this->target; |
||
76 | } |
||
77 | |||
78 | // @phpstan-ignore-next-line |
||
79 | public function getParams() |
||
80 | { |
||
81 | if ($this->action === 'create' || $this->action === 'read' || $this->action === 'update') { |
||
82 | return $this->getParamsObject(); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | protected function processJson(string $json): void |
||
87 | { |
||
88 | $decoded = json_decode($json); |
||
89 | $this->action = $decoded->action ?? ''; |
||
90 | $this->setTarget($decoded->target ?? ''); |
||
91 | |||
92 | if (property_exists($decoded, 'entity') && $decoded->entity !== null) { |
||
93 | $id = (int) $decoded->entity->id; |
||
94 | if ($id === 0) { |
||
95 | $id = null; |
||
96 | } |
||
97 | $this->Entity = $this->getEntity($decoded->entity->type, $id); |
||
98 | } |
||
99 | $this->id = $this->setId((int) ($decoded->id ?? 0)); |
||
100 | $this->Model = $this->buildModel($decoded->model ?? ''); |
||
101 | $this->content = $decoded->content ?? ''; |
||
102 | if (property_exists($decoded, 'extraParams')) { |
||
103 | $this->extra = (array) $decoded->extraParams; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | abstract protected function process(Request $request): void; |
||
108 | |||
109 | protected function setTarget(string $target): void |
||
110 | { |
||
111 | $this->target = Check::target($target); |
||
112 | } |
||
113 | |||
114 | protected function getEntity(string $type, ?int $itemId = null): AbstractEntity |
||
115 | { |
||
116 | if ($type === 'experiment') { |
||
117 | return new Experiments($this->Users, $itemId); |
||
118 | } elseif ($type === 'template') { |
||
119 | return new Templates($this->Users, $itemId); |
||
120 | } elseif ($type === 'itemtype') { |
||
121 | return new ItemsTypes($this->Users, $itemId); |
||
122 | } |
||
123 | return new Items($this->Users, $itemId); |
||
124 | } |
||
125 | |||
126 | protected function buildModel(string $model): CrudInterface | Users |
||
127 | { |
||
128 | switch ($model) { |
||
129 | case 'apikey': |
||
130 | return new ApiKeys($this->Users, $this->id); |
||
131 | case 'status': |
||
132 | return new Status($this->Users->team, $this->id); |
||
133 | case 'comment': |
||
134 | return new Comments($this->Entity, new Email(Config::getConfig(), $this->Users), $this->id); |
||
135 | case 'link': |
||
136 | return new Links($this->Entity, $this->id); |
||
137 | case 'favtag': |
||
138 | return new FavTags($this->Users, $this->id); |
||
139 | case 'step': |
||
140 | return new Steps($this->Entity, $this->id); |
||
141 | case 'unfinishedsteps': |
||
142 | return new UnfinishedSteps($this->Entity); |
||
143 | case 'upload': |
||
144 | return new Uploads($this->Entity, $this->id); |
||
145 | case 'privacypolicy': |
||
146 | return new PrivacyPolicy(Config::getConfig()); |
||
147 | case 'teamgroup': |
||
148 | return new TeamGroups($this->Users, $this->id); |
||
149 | case 'tag': |
||
150 | return new Tags($this->Entity, $this->id); |
||
151 | case 'experiment': |
||
152 | case 'item': |
||
153 | case 'template': |
||
154 | case 'itemtype': |
||
155 | return $this->Entity; |
||
156 | case 'todolist': |
||
157 | return new Todolist((int) $this->Users->userData['userid'], $this->id); |
||
158 | case 'user': |
||
159 | return $this->Users; |
||
160 | default: |
||
161 | throw new IllegalActionException('Bad model'); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | protected function setId(?int $id): ?int |
||
175 | } |
||
176 | |||
177 | // @phpstan-ignore-next-line |
||
178 | private function getParamsObject() |
||
179 | { |
||
180 | if ($this->Model instanceof Comments || |
||
181 | $this->Model instanceof Todolist || |
||
182 | $this->Model instanceof Links || |
||
183 | $this->Model instanceof FavTags || |
||
221 | } |
||
222 | } |
||
224 |