Passed
Push — master ( 1ae59d...620d3f )
by Konrad
04:55
created

OutputHelper::printTodos()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 16.0575

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 23
c 1
b 0
f 1
dl 0
loc 42
ccs 16
cts 23
cp 0.6957
rs 6.9666
cc 12
nc 10
nop 3
crap 16.0575

How to fix   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
        $output->writeln('');
34 1
        foreach ($todos as $dir => $entries) {
35 1
            $dir = (string) $dir;
36 1
            $dirIsShown = false;
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
                $output->writeln('> '.$dir);
43
                $dirIsShown = true;
44 1
            } elseif ($this->dirHasFilesWithTodos($todos, $dir)) {
45 1
                $output->writeln('> '.$dir);
46 1
                $dirIsShown = true;
47
            }
48
49 1
            if (0 == \count($entries)) {
50
                /*
51
                 * show_no_files_info
52
                 */
53
                if ($config['show_no_files_info'] && $dirIsShown) {
54
                    $output->writeln('      <fg=cyan>No files found.</>');
55
                }
56
                continue;
57
            }
58
59 1
            foreach ($entries as $file => $todoList) {
60 1
                if (0 == \count($todoList)) {
61
                    if ($config['show_files_with_no_todos']) {
62
                        $output->writeln('     '.$file);
63
                    }
64
                } else {
65 1
                    $output->writeln('     '.$file);
66 1
                    foreach ($todoList as $todo) {
67 1
                        $output->writeln('     - '.$todo['message']);
68
                    }
69
                }
70
            }
71
        }
72 1
        $output->writeln('');
73 1
    }
74
}
75