Conditions | 13 |
Paths | 57 |
Total Lines | 61 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
90 | protected function execute(InputInterface $input, OutputInterface $output) |
||
91 | { |
||
92 | $this->detectMagento($output, true); |
||
93 | if (!$this->initMagento()) { |
||
94 | return; |
||
95 | } |
||
96 | |||
97 | /** @var \Zend_Cache_Core $lowLevelFrontend */ |
||
98 | if ($input->hasOption('fpc') && $input->getOption('fpc')) { |
||
99 | $lowLevelFrontend = $this->fpc->getLowLevelFrontend(); |
||
100 | } else { |
||
101 | $lowLevelFrontend = $this->cache->getFrontend()->getLowLevelFrontend(); |
||
102 | } |
||
103 | |||
104 | $table = []; |
||
105 | $cacheIds = $lowLevelFrontend->getIds(); |
||
106 | $filterId = $input->getOption('filter-id'); |
||
107 | $filterTag = $input->getOption('filter-tag'); |
||
108 | $mTime = $input->getOption('mtime'); |
||
109 | $tags = $input->getOption('tags'); |
||
110 | foreach ($cacheIds as $cacheId) { |
||
111 | if ($filterId !== null && |
||
112 | !stristr($cacheId, $filterId)) { |
||
113 | continue; |
||
114 | } |
||
115 | |||
116 | $metadata = $lowLevelFrontend->getMetadatas($cacheId); |
||
117 | if ($filterTag !== null && |
||
118 | !$this->isTagFiltered($metadata, $input)) { |
||
119 | continue; |
||
120 | } |
||
121 | |||
122 | $row = [ |
||
123 | $cacheId, |
||
124 | date('Y-m-d H:i:s', $metadata['expire']), |
||
125 | ]; |
||
126 | |||
127 | if ($mTime) { |
||
128 | $row[] = date('Y-m-d H:i:s', $metadata['mtime']); |
||
129 | } |
||
130 | |||
131 | if ($tags) { |
||
132 | $row[] = implode(', ', $metadata['tags']); |
||
133 | } |
||
134 | |||
135 | $table[] = $row; |
||
136 | } |
||
137 | |||
138 | $headers = ['ID', 'EXPIRE']; |
||
139 | if ($mTime) { |
||
140 | $headers[] = 'MTIME'; |
||
141 | } |
||
142 | if ($tags) { |
||
143 | $headers[] = 'TAGS'; |
||
144 | } |
||
145 | |||
146 | $this |
||
|
|||
147 | ->getHelper('table') |
||
148 | ->setHeaders($headers) |
||
149 | ->renderByFormat($output, $table, $input->getOption('format')); |
||
150 | } |
||
151 | |||
167 |
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: