Conditions | 8 |
Paths | 16 |
Total Lines | 53 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
72 | public function upload(ResponsiveImageInterface $image) |
||
73 | { |
||
74 | // Dispatch pre-upload event. |
||
75 | if (!empty($this->eventDispatcher)) { |
||
76 | $uploaderEvent = new UploaderEvent($this); |
||
77 | $this->eventDispatcher->dispatch(UploaderEvents::UPLOADER_PRE_UPLOAD, $uploaderEvent); |
||
78 | } |
||
79 | |||
80 | $this->file = $image->getFile(); |
||
81 | |||
82 | // Use UploadedFile's inbuilt validation and allow |
||
83 | // implementation specific custom checks on uploaded file |
||
84 | if ($this->file->isValid() && $this->isValid()) { |
||
85 | |||
86 | // Alter name for uniqueness |
||
87 | $path = $this->formatPath(); |
||
88 | |||
89 | $info = getimagesize($this->file); |
||
90 | list($length, $height) = $info; |
||
91 | |||
92 | // Save the actual file to the filesystem. |
||
93 | $stream = fopen($this->file->getRealPath(), 'r+'); |
||
94 | $this->fileSystem->writeStream($path, $stream); |
||
95 | fclose($stream); |
||
96 | |||
97 | $image->setPath($path); |
||
98 | $image->setHeight($length); |
||
99 | $image->setWidth($height); |
||
100 | |||
101 | // If the image has a setFileSystem method set the filesystem data. |
||
102 | if (method_exists($image, 'setFileSystem')) { |
||
103 | $storageData = $this->getStorageDataFormFileSystem(); |
||
104 | if (!empty($storageData)) { |
||
105 | $image->setFileSystem(serialize($storageData)); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | // Clean up the file property as you won't need it anymore. |
||
110 | $this->file = null; |
||
111 | $image->setFile(null); |
||
112 | |||
113 | // Dispatch uploaded event |
||
114 | if (!empty($uploaderEvent)) { |
||
115 | $this->eventDispatcher->dispatch(UploaderEvents::UPLOADER_UPLOADED, $uploaderEvent); |
||
116 | } |
||
117 | } |
||
118 | else { |
||
119 | $error = empty($this->error) ? $this->file->getErrorMessage() : $this->error; |
||
120 | throw new FileException( |
||
121 | $error |
||
122 | ); |
||
123 | } |
||
124 | } |
||
125 | |||
186 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: