Passed
Pull Request — master (#35)
by James
02:02
created

MarkdownFormatter::write()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 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
9
final class MarkdownFormatter implements OutputFormatter
10
{
11
    /**
12
     * @var string
13
     */
14
    private $outputFilename;
15
16
    public function __construct(string $outputFilename)
17
    {
18
        $this->outputFilename = $outputFilename;
19
    }
20
21
    public function write(Changes $changes) : void
22
    {
23
        $arrayOfChanges = $changes->getIterator()->getArrayCopy();
24
25
        file_put_contents(
26
            $this->outputFilename,
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