| Conditions | 11 |
| Paths | 15 |
| Total Lines | 78 |
| Code Lines | 43 |
| 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 |
||
| 24 | function populate_rt($table, $max) |
||
| 25 | { |
||
| 26 | if ($table === 'releases_rt') { |
||
| 27 | DB::statement('SET SESSION group_concat_max_len=16384;'); |
||
| 28 | $query = 'SELECT r.id, r.name, r.searchname, r.fromname, IFNULL(GROUP_CONCAT(rf.name SEPARATOR " "),"") filename |
||
| 29 | FROM releases r |
||
| 30 | LEFT JOIN release_files rf ON r.id = rf.releases_id |
||
| 31 | WHERE r.id > %d |
||
| 32 | GROUP BY r.id |
||
| 33 | ORDER BY r.id ASC |
||
| 34 | LIMIT %d'; |
||
| 35 | |||
| 36 | $totals = Release::fromQuery('SELECT COUNT(id) AS c, MIN(id) AS min FROM releases')->first(); |
||
| 37 | if (! $totals) { |
||
| 38 | exit("Could not get database information for releases table.\n"); |
||
| 39 | } |
||
| 40 | $total = $totals->c; |
||
| 41 | $minId = $totals->min; |
||
| 42 | } |
||
| 43 | |||
| 44 | if ($table === 'predb_rt') { |
||
| 45 | DB::statement('SET SESSION group_concat_max_len=16384;'); |
||
| 46 | $query = 'SELECT id, title, filename |
||
| 47 | FROM predb |
||
| 48 | WHERE id > %d |
||
| 49 | GROUP BY id |
||
| 50 | ORDER BY id ASC |
||
| 51 | LIMIT %d'; |
||
| 52 | |||
| 53 | $totals = Predb::fromQuery('SELECT COUNT(id) AS c, MIN(id) AS min FROM predb')->first(); |
||
| 54 | if (! $totals) { |
||
| 55 | exit("Could not get database information for predb table.\n"); |
||
| 56 | } |
||
| 57 | $total = $totals->c; |
||
| 58 | $minId = $totals->min; |
||
| 59 | } |
||
| 60 | |||
| 61 | $sphinx = new SphinxSearch(); |
||
| 62 | $lastId = $minId - 1; |
||
| 63 | echo "[Starting to populate sphinx RT index $table with $total releases.]".PHP_EOL; |
||
| 64 | for ($i = $minId; $i <= ($total + $max + $minId); $i += $max) { |
||
| 65 | $rows = DB::select(sprintf($query, $lastId, $max)); |
||
| 66 | if ($rows === 0) { |
||
| 67 | continue; |
||
| 68 | } |
||
| 69 | |||
| 70 | foreach ($rows as $row) { |
||
| 71 | if ($row->id > $lastId) { |
||
| 72 | $lastId = $row->id; |
||
| 73 | } |
||
| 74 | switch ($table) { |
||
| 75 | case 'releases_rt': |
||
| 76 | $sphinx->insertRelease( |
||
| 77 | [ |
||
| 78 | 'id' => $row->id, |
||
| 79 | 'name' => $row->name, |
||
| 80 | 'searchname' => $row->searchname, |
||
| 81 | 'fromname' => $row->fromname, |
||
| 82 | 'filename' => $row->filename, |
||
| 83 | ] |
||
| 84 | ); |
||
| 85 | break; |
||
| 86 | |||
| 87 | case 'predb_rt': |
||
| 88 | $sphinx->insertPredb( |
||
| 89 | [ |
||
| 90 | 'id' => $row->id, |
||
| 91 | 'title' => $row->title, |
||
| 92 | 'filename' => $row->filename, |
||
| 93 | ] |
||
| 94 | ); |
||
| 95 | break; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | echo '.'; |
||
| 100 | } |
||
| 101 | echo "\n[Done]\n"; |
||
| 102 | } |
||
| 103 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.