| Conditions | 10 |
| Paths | 24 |
| Total Lines | 62 |
| 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 |
||
| 23 | public function generate(PagesCollection $pagesCollection, \Closure $messageCallback) |
||
| 24 | { |
||
| 25 | $generatedPages = new PagesCollection('sections'); |
||
|
|
|||
| 26 | $sectionsList = []; |
||
| 27 | |||
| 28 | // identify sections |
||
| 29 | /* @var $page Page */ |
||
| 30 | foreach ($pagesCollection as $page) { |
||
| 31 | if ($page->getSection()) { |
||
| 32 | // ie: ['blog'][0]['blog/post-1'] |
||
| 33 | $sectionsList[$page->getSection()][] = $page->getId(); |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | // sections collections |
||
| 38 | $sections = []; |
||
| 39 | foreach ($sectionsList as $sectionName => $pagesList) { |
||
| 40 | if (!array_key_exists($sectionName, $sections)) { |
||
| 41 | $sections[$sectionName] = new PagesCollection($sectionName); |
||
| 42 | foreach ($pagesList as $pageId) { |
||
| 43 | $sections[$sectionName]->add($pagesCollection->get($pageId)); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | // DEBUG |
||
| 49 | //print_r($sections2); |
||
| 50 | //print_r($section3); |
||
| 51 | foreach ($section3 as $section => $collection) { |
||
| 52 | //echo $section."\n"; |
||
| 53 | $collection->sortByDate(); |
||
| 54 | //echo $collection->getId()."\n"; |
||
| 55 | //echo $collection; |
||
| 56 | print_r($collection); |
||
| 57 | } |
||
| 58 | die(); |
||
| 59 | |||
| 60 | // adds section pages to collection |
||
| 61 | if (count($sections) > 0) { |
||
| 62 | $menuWeight = 100; |
||
| 63 | foreach ($sections as $section => $pages) { |
||
| 64 | $pageId = Page::slugify($section); |
||
| 65 | if (!$pagesCollection->has($pageId)) { |
||
| 66 | usort($pages, 'Cecil\Util::sortByDate'); |
||
| 67 | $page = (new Page()) |
||
| 68 | ->setId($pageId) |
||
| 69 | ->setPathname($pageId) |
||
| 70 | ->setVariable('title', ucfirst($section)) |
||
| 71 | ->setType(Type::SECTION) |
||
| 72 | ->setVariable('pages', $pages) |
||
| 73 | ->setVariable('date', reset($pages)->getVariable('date')) |
||
| 74 | ->setVariable('menu', [ |
||
| 75 | 'main' => ['weight' => $menuWeight], |
||
| 76 | ]); |
||
| 77 | $generatedPages->add($page); |
||
| 78 | } |
||
| 79 | $menuWeight += 10; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | return $generatedPages; |
||
| 84 | } |
||
| 85 | } |
||
| 86 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.