Conditions | 13 |
Paths | 43 |
Total Lines | 83 |
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 |
||
73 | protected function process() |
||
74 | { |
||
75 | |||
76 | // query whether or not the media gallery has to be cleaned up |
||
77 | if ($this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_MEDIA_GALLERY) && |
||
78 | $this->getSubject()->getConfiguration()->getParam(ConfigurationKeys::CLEAN_UP_MEDIA_GALLERY) |
||
79 | ) { |
||
80 | // initialize the array for the actual images |
||
81 | $actualImageNames = array(); |
||
82 | |||
83 | // iterate over the available image fields |
||
84 | foreach (array_keys($this->getImageTypes()) as $imageColumnName) { |
||
85 | if ($this->hasValue($imageColumnName) && !in_array($imageName = $this->getValue($imageColumnName), $actualImageNames)) { |
||
86 | $actualImageNames[] = $imageName; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | // query whether or not, we've additional images |
||
91 | if ($additionalImages = $this->getValue(ColumnKeys::ADDITIONAL_IMAGES, null, array($this, 'explode'))) { |
||
92 | foreach ($additionalImages as $additionalImageName) { |
||
93 | // do nothing if the image has already been added, e. g. it is the base image |
||
94 | if (in_array($additionalImageName, $actualImageNames)) { |
||
95 | continue; |
||
96 | } |
||
97 | |||
98 | // else, add the image to the array |
||
99 | $actualImageNames[] = $additionalImageName; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | // load the existing media gallery entities for the produdct with the given SKU |
||
104 | $existingProductMediaGalleries = $this->getProductMediaProcessor() |
||
105 | ->getProductMediaGalleriesBySku($sku = $this->getValue(ColumnKeys::SKU)); |
||
106 | |||
107 | // remove the images that are NOT longer available in the CSV file |
||
108 | foreach ($existingProductMediaGalleries as $existingProductMediaGallery) { |
||
109 | // do nothing if the file still exists |
||
110 | if (in_array($existingImageName = $existingProductMediaGallery[MemberNames::VALUE], $actualImageNames)) { |
||
111 | continue; |
||
112 | } |
||
113 | |||
114 | try { |
||
115 | // remove the old image from the database |
||
116 | $this->getProductMediaProcessor() |
||
117 | ->deleteProductMediaGallery(array(MemberNames::VALUE_ID => $existingProductMediaGallery[MemberNames::VALUE_ID])); |
||
118 | |||
119 | // log a debug message that the image has been removed |
||
120 | $this->getSubject() |
||
121 | ->getSystemLogger() |
||
122 | ->warning( |
||
123 | $this->getSubject()->appendExceptionSuffix( |
||
124 | sprintf( |
||
125 | 'Successfully removed image "%s" from media gallery for product with SKU "%s"', |
||
126 | $existingImageName, |
||
127 | $sku |
||
128 | ) |
||
129 | ) |
||
130 | ); |
||
131 | } catch (\Exception $e) { |
||
132 | // log a warning if debug mode has been enabled and the file is NOT available |
||
133 | if ($this->getSubject()->isDebugMode()) { |
||
134 | $this->getSubject() |
||
135 | ->getSystemLogger() |
||
136 | ->warning($this->getSubject()->appendExceptionSuffix($e->getMessage())); |
||
137 | } else { |
||
138 | throw $e; |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | |||
143 | // log a message that the images has been cleaned-up |
||
144 | $this->getSubject() |
||
145 | ->getSystemLogger() |
||
146 | ->debug( |
||
147 | $this->getSubject()->appendExceptionSuffix( |
||
148 | sprintf( |
||
149 | 'Successfully cleaned-up media gallery for product with SKU "%s"', |
||
150 | $sku |
||
151 | ) |
||
152 | ) |
||
153 | ); |
||
154 | } |
||
155 | } |
||
156 | |||
179 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: