Passed
Push — master ( 4df9e3...45d8f4 )
by Konrad
11:35
created

OutputHelper::printTodos()   C

Complexity

Conditions 13
Paths 10

Size

Total Lines 59
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 29.3962

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 38
c 2
b 1
f 1
dl 0
loc 59
ccs 20
cts 37
cp 0.5405
rs 6.6166
cc 13
nc 10
nop 3
crap 29.3962

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Todolo\Helper;
6
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class OutputHelper
10
{
11
    /**
12
     * @param array<int|string, array<array>> $todos
13
     */
14 1
    protected function dirHasFilesWithTodos(array $todos, string $dirToCheck): bool
15
    {
16 1
        foreach ($todos as $dir => $entries) {
17 1
            foreach ($entries as $todoList) {
18 1
                if ($dir == $dirToCheck && 0 < \count($todoList)) {
19 1
                    return true;
20
                }
21
            }
22
        }
23
24
        return false;
25
    }
26
27
    /**
28
     * @param array<int|string, array<array>> $todos
29
     * @param array<int|string, mixed>        $config
30
     */
31 1
    public function printTodos(OutputInterface $output, array $todos, array $config): void
32
    {
33 1
        foreach ($todos as $dir => $entries) {
34 1
            $dir = (string) $dir;
35 1
            $showDir = false;
36 1
            $firstFileInDir = true;
37
38
            /*
39
             * if show_empty_dir_info is ON only show dir name if we have TODOs
40
             */
41 1
            if ($config['show_empty_dir'] && !$this->dirHasFilesWithTodos($todos, $dir)) {
42
                $showDir = true;
43 1
            } elseif ($this->dirHasFilesWithTodos($todos, $dir)) {
44 1
                $showDir = true;
45
            }
46
47 1
            if (0 == \count($entries)) {
48
                /*
49
                 * show_no_files_info
50
                 */
51
                if ($config['show_no_files_info'] && $showDir) {
52
                    $output->writeln('');
53
                    $output->writeln('-----------------------------------------------------');
54
                    $output->writeln('<info>'.$dir.'</info>');
55
                    $output->writeln('-----------------------------------------------------');
56
                    $output->writeln('<comment>No TODOs found.</comment>');
57
                    $output->writeln('-----------------------------------------------------');
58
                }
59
                continue;
60
            }
61
62 1
            foreach ($entries as $file => $todoList) {
63 1
                if (0 == \count($todoList)) {
64
                    if ($config['show_files_with_no_todos']) {
65
                        $output->writeln('');
66
                        $output->writeln('-----------------------------------------------------');
67
                        $output->writeln('<info>'.$dir.'</info>'.$file);
68
                        $output->writeln('-----------------------------------------------------');
69
                        $output->writeln('<comment>No TODOs found.</comment>');
70
                        $output->writeln('-----------------------------------------------------');
71
                    }
72
                } else {
73 1
                    if (!$firstFileInDir) {
74
                        $output->writeln('');
75
                    } else {
76 1
                        $firstFileInDir = false;
77
                    }
78
79 1
                    $output->writeln('');
80 1
                    $output->writeln('-----------------------------------------------------');
81 1
                    $output->writeln('<info>'.$dir.'</info>'.$file);
82 1
                    $output->writeln('-----------------------------------------------------');
83 1
                    foreach ($todoList as $todo) {
84 1
                        $output->writeln('- '.$todo['message']);
85
                    }
86
                }
87
            }
88
        }
89 1
        $output->writeln('');
90 1
    }
91
}
92