Total Complexity | 46 |
Total Lines | 375 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Complex classes like FormDataReader 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 FormDataReader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class FormDataReader |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * documentTypeRepository |
||
28 | * |
||
29 | * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository |
||
30 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
31 | */ |
||
32 | protected $documentTypeRepository = null; |
||
33 | |||
34 | /** |
||
35 | * metadataPageRepository |
||
36 | * |
||
37 | * @var \EWW\Dpf\Domain\Repository\MetadataPageRepository |
||
38 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
39 | */ |
||
40 | protected $metadataPageRepository = null; |
||
41 | |||
42 | /** |
||
43 | * metadataGroupRepository |
||
44 | * |
||
45 | * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository |
||
46 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
47 | */ |
||
48 | protected $metadataGroupRepository = null; |
||
49 | |||
50 | /** |
||
51 | * metadataObjectRepository |
||
52 | * |
||
53 | * @var \EWW\Dpf\Domain\Repository\MetadataObjectRepository |
||
54 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
55 | */ |
||
56 | protected $metadataObjectRepository = null; |
||
57 | |||
58 | /** |
||
59 | * fileRepository |
||
60 | * |
||
61 | * @var \EWW\Dpf\Domain\Repository\FileRepository |
||
62 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
63 | */ |
||
64 | protected $fileRepository = null; |
||
65 | |||
66 | /** |
||
67 | * documentRepository |
||
68 | * |
||
69 | * @var \EWW\Dpf\Domain\Repository\DocumentRepository |
||
70 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
71 | */ |
||
72 | protected $documentRepository = null; |
||
73 | |||
74 | /** |
||
75 | * objectManager |
||
76 | * |
||
77 | * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface |
||
78 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
79 | */ |
||
80 | protected $objectManager; |
||
81 | |||
82 | /** |
||
83 | * |
||
84 | * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface |
||
85 | * @TYPO3\CMS\Extbase\Annotation\Inject |
||
86 | */ |
||
87 | protected $configurationManager; |
||
88 | |||
89 | /** |
||
90 | * formData |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $formData; |
||
95 | |||
96 | /** |
||
97 | * documentType |
||
98 | * |
||
99 | * @var |
||
100 | */ |
||
101 | protected $documentType; |
||
102 | |||
103 | /** |
||
104 | * uploadPath |
||
105 | * |
||
106 | * @var |
||
107 | */ |
||
108 | protected $uploadPath; |
||
109 | |||
110 | /** |
||
111 | * basePath |
||
112 | * |
||
113 | * @var |
||
114 | */ |
||
115 | protected $uploadBaseUrl; |
||
116 | |||
117 | public function __construct() |
||
118 | { |
||
119 | |||
120 | $uploadFileUrl = new \EWW\Dpf\Helper\UploadFileUrl; |
||
121 | |||
122 | $this->uploadBaseUrl = $uploadFileUrl->getUploadUrl() . "/"; |
||
123 | |||
124 | $this->uploadPath = Environment::getPublicPath() . "/" . $uploadFileUrl->getDirectory() . "/"; |
||
125 | |||
126 | } |
||
127 | |||
128 | /** |
||
129 | * |
||
130 | * @param array $formData |
||
131 | */ |
||
132 | public function setFormData($formData) |
||
133 | { |
||
134 | $this->formData = $formData; |
||
135 | $this->documentType = $this->documentTypeRepository->findByUid($formData['type']); |
||
136 | } |
||
137 | |||
138 | protected function getFields() |
||
139 | { |
||
140 | |||
141 | $fields = array(); |
||
142 | |||
143 | if (is_array($this->formData['metadata'])) { |
||
144 | foreach ($this->formData['metadata'] as $key => $value) { |
||
145 | $formField = new \EWW\Dpf\Helper\FormField($key, $value); |
||
146 | $fields[] = $formField; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | return $fields; |
||
151 | } |
||
152 | |||
153 | protected function getDeletedFiles() |
||
154 | { |
||
155 | $deletedFiles = array(); |
||
156 | |||
157 | if (is_array($this->formData['deleteFile'])) { |
||
158 | foreach ($this->formData['deleteFile'] as $key => $value) { |
||
159 | |||
160 | $file = $this->fileRepository->findByUid($value); |
||
161 | |||
162 | // Deleting the primary file is not allowed. |
||
163 | // if (!$file->isPrimaryFile()) { |
||
164 | // $deletedFiles[] = $file; |
||
165 | // } |
||
166 | |||
167 | $deletedFiles[] = $file; |
||
168 | } |
||
169 | } |
||
170 | |||
171 | return $deletedFiles; |
||
172 | } |
||
173 | |||
174 | public function uploadError() |
||
196 | } |
||
197 | |||
198 | protected function getUploadedFile($tmpFile, $primary = false, \EWW\Dpf\Domain\Model\File $file = null) |
||
199 | { |
||
200 | |||
201 | if (empty($file)) { |
||
202 | $file = $this->objectManager->get(File::class); |
||
203 | } |
||
204 | |||
205 | $fileName = uniqid(time(), true); |
||
206 | |||
207 | \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move($tmpFile['tmp_name'], $this->uploadPath . $fileName); |
||
208 | |||
209 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
||
210 | $contentType = finfo_file($finfo, $this->uploadPath . $fileName); |
||
211 | finfo_close($finfo); |
||
212 | |||
213 | $file->setContentType($contentType); |
||
214 | |||
215 | $file->setTitle($tmpFile['name']); |
||
216 | $file->setLink($fileName); |
||
217 | $file->setPrimaryFile($primary); |
||
218 | $file->setFileIdentifier(uniqid(time(), true)); |
||
219 | |||
220 | if ($primary) { |
||
221 | if ($file->getDatastreamIdentifier()) { |
||
222 | $file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_CHANGED); |
||
223 | } else { |
||
224 | $file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_ADDED); |
||
225 | } |
||
226 | } else { |
||
227 | $file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_ADDED); |
||
228 | } |
||
229 | |||
230 | return $file; |
||
231 | } |
||
232 | |||
233 | public function getDocumentForm() |
||
398 | } |
||
399 | } |
||
400 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.