Conditions | 23 |
Paths | 240 |
Total Lines | 67 |
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 |
||
56 | public function run($request) |
||
57 | { |
||
58 | if (isset($_REQUEST['start']) && intval($_REQUEST['start'])) { |
||
59 | $this->start = intval($_REQUEST['start']); |
||
60 | } |
||
61 | if (isset($_REQUEST['productid']) && intval($_REQUEST['productid'])) { |
||
62 | $this->productID = intval($_REQUEST['productid']); |
||
63 | } |
||
64 | if ($this->productManyManyField) { |
||
65 | $products = Product::get()->limit($this->limit, $this->start); |
||
66 | if ($this->productID) { |
||
67 | $products = $products->filter(array('ID' => $this->productID)); |
||
68 | } |
||
69 | if ($products->count()) { |
||
70 | foreach ($products as $product) { |
||
71 | if ($product->InternalItemID) { |
||
72 | if ($product->hasMethod($this->productManyManyField)) { |
||
73 | $whereStringArray[] = $product->InternalItemID; |
||
|
|||
74 | for ($i = 0; $i < 10; ++$i) { |
||
75 | for ($j = 0; $j < 10; ++$j) { |
||
76 | $number = strval($i).strval($j); |
||
77 | $whereStringArray[] = $product->InternalItemID.'_'.$number; |
||
78 | } |
||
79 | } |
||
80 | $images = File::get() |
||
81 | ->filter(array('Name:PartialMatch' => $whereStringArray)); |
||
82 | if ($images->count()) { |
||
83 | $method = $this->productManyManyField; |
||
84 | $collection = $product->$method(); |
||
85 | foreach ($images as $image) { |
||
86 | if (is_a($image, Object::getCustomClass('Image')) && $image->ClassName != Object::getCustomClass('Product_Image')) { |
||
87 | $image = $image->newClassInstance('Product_Image'); |
||
88 | $image->write(); |
||
89 | } |
||
90 | $collection->add($image); |
||
91 | if ($this->verbose) { |
||
92 | DB::alteration_message('Adding image '.$image->Name.' to '.$product->Title, 'created'); |
||
93 | } |
||
94 | } |
||
95 | } else { |
||
96 | if ($this->verbose) { |
||
97 | DB::alteration_message('No images where found for product with Title <i>'.$product->Title.'</i>: no images could be added.'); |
||
98 | } |
||
99 | } |
||
100 | } else { |
||
101 | if ($this->verbose) { |
||
102 | DB::alteration_message('The method <i>'.$this->productManyManyField.'</i> does not exist on <i>'.$product->Title.' ('.$product->ClassName.')</i>: no images could be added.'); |
||
103 | } |
||
104 | } |
||
105 | } else { |
||
106 | if ($this->verbose) { |
||
107 | DB::alteration_message('No InternalItemID set for <i>'.$product->Title.'</i>: no images could be added.'); |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 | $productCount = Product::get()->count(); |
||
112 | if ($this->limit < $productCount) { |
||
113 | $controller = Controller::curr(); |
||
114 | $controller->redirect($this->nextBatchLink()); |
||
115 | } |
||
116 | } |
||
117 | } else { |
||
118 | if ($this->verbose) { |
||
119 | DB::alteration_message('No product Many-2-Many method specified. No further action taken. '); |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
151 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.