Conditions | 8 |
Paths | 8 |
Total Lines | 51 |
Code Lines | 26 |
Lines | 27 |
Ratio | 52.94 % |
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 |
||
37 | function getOutput() { |
||
38 | $entries = []; |
||
39 | |||
40 | $addonsCache = new Cache('oscommerce_website-addons-latest5'); |
||
41 | |||
42 | View Code Duplication | if ($addonsCache->exists(360)) { |
|
43 | $entries = $addonsCache->get(); |
||
44 | } else { |
||
45 | $response = HTTP::getResponse(['url' => 'https://www.oscommerce.com/index.php?RPC&GetLatestAddons']); |
||
46 | |||
47 | if (!empty($response)) { |
||
48 | $response = json_decode($response, true); |
||
49 | |||
50 | if (is_array($response) && (count($response) === 5)) { |
||
51 | $entries = $response; |
||
52 | } |
||
53 | } |
||
54 | |||
55 | $addonsCache->save($entries); |
||
56 | } |
||
57 | |||
58 | $output = '<table class="table table-hover"> |
||
59 | <thead> |
||
60 | <tr class="info"> |
||
61 | <th>' . MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_TITLE . '</th> |
||
62 | <th class="text-right">' . MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_DATE . '</th> |
||
63 | </tr> |
||
64 | </thead> |
||
65 | <tbody>'; |
||
66 | |||
67 | View Code Duplication | if (is_array($entries) && (count($entries) === 5)) { |
|
68 | foreach ($entries as $item) { |
||
69 | $output .= ' <tr> |
||
70 | <td><a href="' . HTML::outputProtected($item['link']) . '" target="_blank">' . HTML::outputProtected($item['title']) . '</a></td> |
||
71 | <td class="text-right" style="white-space: nowrap;">' . HTML::outputProtected(DateTime::toShort($item['date'])) . '</td> |
||
72 | </tr>'; |
||
73 | } |
||
74 | } else { |
||
75 | $output .= ' <tr> |
||
76 | <td colspan="2">' . MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_FEED_ERROR . '</td> |
||
77 | </tr>'; |
||
78 | } |
||
79 | |||
80 | $output .= ' <tr> |
||
81 | <td class="text-right" colspan="2"><a href="http://addons.oscommerce.com" target="_blank" title="' . HTML::outputProtected(MODULE_ADMIN_DASHBOARD_LATEST_ADDONS_ICON_SITE) . '"><span class="fa fa-fw fa-home"></span></a></td> |
||
82 | </tr> |
||
83 | </tbody> |
||
84 | </table>'; |
||
85 | |||
86 | return $output; |
||
87 | } |
||
88 | |||
131 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: