| Conditions | 6 |
| Paths | 12 |
| Total Lines | 58 |
| 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 |
||
| 47 | function showRandomquoteBlockViews($options) |
||
| 48 | { |
||
| 49 | $moduleDirName = basename(dirname(__DIR__)); |
||
| 50 | // require_once XOOPS_ROOT_PATH . '/modules/randomquote/class/quotes.php'; |
||
| 51 | $utility = new randomquote\Utility(); |
||
| 52 | |||
| 53 | $quotes = []; |
||
| 54 | $type_block = $options[0]; |
||
| 55 | $nb_quotes = $options[1]; |
||
| 56 | $length_title = (int)$options[2]; |
||
| 57 | |||
| 58 | $quotesHandler = new randomquote\QuotesHandler($GLOBALS['xoopsDB']); |
||
| 59 | $criteria = new CriteriaCompo(); |
||
| 60 | array_shift($options); |
||
| 61 | array_shift($options); |
||
| 62 | array_shift($options); |
||
| 63 | |||
| 64 | switch ($type_block) { |
||
| 65 | // for block: quotes recent |
||
| 66 | case 'recent': |
||
| 67 | $criteria->add(new Criteria('online', 1)); |
||
| 68 | // $criteria->setSort("quotes_date_created"); |
||
| 69 | $criteria->setSort('id'); |
||
| 70 | $criteria->setOrder('DESC'); |
||
| 71 | break; |
||
| 72 | // for block: quotes today's |
||
| 73 | case 'day': |
||
| 74 | $criteria->add(new Criteria('online', 1)); |
||
| 75 | // $criteria->add(new Criteria("quotes_date_created", strtotime(date("Y/m/d")), ">=")); |
||
| 76 | // $criteria->add(new Criteria("quotes_date_created", strtotime(date("Y/m/d")) + 86400, "<=")); |
||
| 77 | // $criteria->setSort("quotes_date_created"); |
||
| 78 | $criteria->setOrder('ASC'); |
||
| 79 | $criteria->setSort('RAND()'); |
||
| 80 | break; |
||
| 81 | // for block: quotes random |
||
| 82 | case 'random': |
||
| 83 | $criteria->add(new Criteria('online', 1)); |
||
| 84 | $criteria->setSort('RAND()'); |
||
| 85 | break; |
||
| 86 | } |
||
| 87 | |||
| 88 | $criteria->setLimit($nb_quotes); |
||
| 89 | $quoteObjsArray = $quotesHandler->getAll($criteria); |
||
| 90 | foreach ($quoteObjsArray as $thisQuote) { |
||
| 91 | if ($length_title > 0) { |
||
| 92 | // $short_quote = xoops_substr($thisQuote->getVar('quote'), 0, $length_title, $trimmarker = '...'); |
||
| 93 | $short_quote = $utility::truncateHtml($thisQuote->getVar('quote'), $length_title, $ending = '...', $exact = false, $considerHtml = true); |
||
| 94 | } else { |
||
| 95 | $short_quote = $thisQuote->getVar('quote'); |
||
| 96 | } |
||
| 97 | $quotes[] = [ |
||
| 98 | 'quote' => $short_quote, |
||
| 99 | 'author' => $thisQuote->getVar('author') |
||
| 100 | ]; |
||
| 101 | } |
||
| 102 | |||
| 103 | return $quotes; |
||
| 104 | } |
||
| 105 | |||
| 134 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: