Complex classes like FileAdder 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 FileAdder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class FileAdder |
||
24 | { |
||
25 | /** @var \Illuminate\Database\Eloquent\Model subject */ |
||
26 | protected $subject; |
||
27 | |||
28 | /** @var \Spatie\MediaLibrary\Filesystem\Filesystem */ |
||
29 | protected $filesystem; |
||
30 | |||
31 | /** @var bool */ |
||
32 | protected $preserveOriginal = false; |
||
33 | |||
34 | /** @var string|\Symfony\Component\HttpFoundation\File\UploadedFile */ |
||
35 | protected $file; |
||
36 | |||
37 | /** @var array */ |
||
38 | protected $properties = []; |
||
39 | |||
40 | /** @var array */ |
||
41 | protected $customProperties = []; |
||
42 | |||
43 | /** @var array */ |
||
44 | protected $manipulations = []; |
||
45 | |||
46 | /** @var string */ |
||
47 | protected $pathToFile; |
||
48 | |||
49 | /** @var string */ |
||
50 | protected $fileName; |
||
51 | |||
52 | /** @var string */ |
||
53 | protected $mediaName; |
||
54 | |||
55 | /** @var string */ |
||
56 | protected $diskName = ''; |
||
57 | |||
58 | /** @var null|callable */ |
||
59 | protected $fileNameSanitizer; |
||
60 | |||
61 | /** @var bool */ |
||
62 | protected $generateResponsiveImages = false; |
||
63 | |||
64 | /** |
||
65 | * @param Filesystem $fileSystem |
||
66 | */ |
||
67 | public function __construct(Filesystem $fileSystem) |
||
68 | { |
||
69 | $this->filesystem = $fileSystem; |
||
70 | |||
71 | $this->fileNameSanitizer = function ($fileName) { |
||
72 | return $this->defaultSanitizer($fileName); |
||
73 | }; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param \Illuminate\Database\Eloquent\Model $subject |
||
78 | * |
||
79 | * @return FileAdder |
||
80 | */ |
||
81 | public function setSubject(Model $subject) |
||
82 | { |
||
83 | $this->subject = $subject; |
||
84 | |||
85 | return $this; |
||
86 | } |
||
87 | |||
88 | /* |
||
89 | * Set the file that needs to be imported. |
||
90 | * |
||
91 | * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file |
||
92 | * |
||
93 | * @return $this |
||
94 | */ |
||
95 | public function setFile($file): self |
||
96 | { |
||
97 | $this->file = $file; |
||
98 | |||
99 | if (is_string($file)) { |
||
100 | $this->pathToFile = $file; |
||
101 | $this->setFileName(pathinfo($file, PATHINFO_BASENAME)); |
||
102 | $this->mediaName = pathinfo($file, PATHINFO_FILENAME); |
||
103 | |||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | if ($file instanceof UploadedFile) { |
||
108 | $this->pathToFile = $file->getPath() . '/' . $file->getFilename(); |
||
109 | $this->setFileName($file->getClientOriginalName()); |
||
110 | $this->mediaName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); |
||
111 | |||
112 | return $this; |
||
113 | } |
||
114 | |||
115 | if ($file instanceof SymfonyFile) { |
||
116 | $this->pathToFile = $file->getPath() . '/' . $file->getFilename(); |
||
117 | $this->setFileName(pathinfo($file->getFilename(), PATHINFO_BASENAME)); |
||
118 | $this->mediaName = pathinfo($file->getFilename(), PATHINFO_FILENAME); |
||
119 | |||
120 | return $this; |
||
121 | } |
||
122 | |||
123 | throw UnknownType::create(); |
||
124 | } |
||
125 | |||
126 | public function preservingOriginal(): self |
||
127 | { |
||
128 | $this->preserveOriginal = true; |
||
129 | |||
130 | return $this; |
||
131 | } |
||
132 | |||
133 | public function usingName(string $name): self |
||
134 | { |
||
135 | return $this->setName($name); |
||
136 | } |
||
137 | |||
138 | public function setName(string $name): self |
||
144 | |||
145 | public function usingFileName(string $fileName): self |
||
146 | { |
||
147 | return $this->setFileName($fileName); |
||
148 | } |
||
149 | |||
150 | public function setFileName(string $fileName): self |
||
156 | |||
157 | public function withCustomProperties(array $customProperties): self |
||
163 | |||
164 | public function withManipulations(array $manipulations): self |
||
170 | |||
171 | public function withProperties(array $properties): self |
||
177 | |||
178 | public function withAttributes(array $properties): self |
||
182 | |||
183 | public function withResponsiveImages(): self |
||
189 | |||
190 | public function addCustomHeaders(array $customRemoteHeaders): self |
||
196 | |||
197 | public function toMediaCollectionOnCloudDisk(string $collectionName = 'default'): Media |
||
201 | |||
202 | public function toMediaCollection(string $collectionName = 'default', string $diskName = ''): Media |
||
243 | |||
244 | protected function determineDiskName(string $diskName, string $collectionName): string |
||
260 | |||
261 | public function defaultSanitizer(string $fileName): string |
||
265 | |||
266 | public function sanitizingFileName(callable $fileNameSanitizer): self |
||
272 | |||
273 | protected function attachMedia(Media $media) |
||
291 | |||
292 | protected function processMediaItem(HasMedia $model, Media $media, FileAdder $fileAdder) |
||
318 | |||
319 | protected function getMediaCollection(string $collectionName): ?MediaCollection |
||
328 | |||
329 | protected function guardAgainstDisallowedFileAdditions(Media $media) |
||
341 | } |
||
342 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.