Completed
Push — master ( a33dc3...ad6b9e )
by Marco
10s
created

MarkdownPipedToSymfonyConsoleFormatter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B write() 0 25 1
A convertFilteredChangesToMarkdownBulletList() 0 7 1
A __construct() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Roave\ApiCompare\Formatter;
5
6
use Roave\ApiCompare\Change;
7
use Roave\ApiCompare\Changes;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
final class MarkdownPipedToSymfonyConsoleFormatter implements OutputFormatter
11
{
12
    /**
13
     * @var OutputInterface
14
     */
15
    private $output;
16
17
    public function __construct(OutputInterface $output)
18
    {
19
        $this->output = $output;
20
    }
21
22
    public function write(Changes $changes) : void
23
    {
24
        $arrayOfChanges = $changes->getIterator()->getArrayCopy();
25
26
        $this->output->writeln(
27
            "# Added\n"
28
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
29
                function (Change $change) : bool {
30
                    return $change->isAdded();
31
                },
32
                ...$arrayOfChanges
33
            ))
34
            . "\n# Changed\n"
35
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
36
                function (Change $change) : bool {
37
                    return $change->isChanged();
38
                },
39
                ...$arrayOfChanges
40
            ))
41
            . "\n# Removed\n"
42
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
43
                function (Change $change) : bool {
44
                    return $change->isRemoved();
45
                },
46
                ...$arrayOfChanges
47
            ))
48
        );
49
    }
50
51
    private function convertFilteredChangesToMarkdownBulletList(callable $filterFunction, Change ...$changes) : array
52
    {
53
        return array_map(
54
            function (Change $change) : string {
55
                return ' - ' . str_replace(['ADDED: ', 'CHANGED: ', 'REMOVED: '], '', trim((string)$change)) . "\n";
56
            },
57
            array_filter($changes, $filterFunction)
58
        );
59
    }
60
}
61