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

MarkdownPipedToSymfonyConsoleFormatter::write()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
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
use Symfony\Component\Console\Output\ConsoleOutputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
final class MarkdownPipedToSymfonyConsoleFormatter implements OutputFormatter
12
{
13
    /**
14
     * @var ConsoleOutputInterface
15
     */
16
    private $output;
17
18
    public function __construct(OutputInterface $output)
19
    {
20
        $this->output = $output;
0 ignored issues
show
Documentation Bug introduced by
$output is of type Symfony\Component\Console\Output\OutputInterface, but the property $output was declared to be of type Symfony\Component\Consol...\ConsoleOutputInterface. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
21
    }
22
23
    public function write(Changes $changes) : void
24
    {
25
        $arrayOfChanges = $changes->getIterator()->getArrayCopy();
26
27
        $this->output->writeln(
28
            "# Added\n"
29
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
30
                function (Change $change) : bool {
31
                    return $change->isAdded();
32
                },
33
                ...$arrayOfChanges
34
            ))
35
            . "\n# Changed\n"
36
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
37
                function (Change $change) : bool {
38
                    return $change->isChanged();
39
                },
40
                ...$arrayOfChanges
41
            ))
42
            . "\n# Removed\n"
43
            . implode('', $this->convertFilteredChangesToMarkdownBulletList(
44
                function (Change $change) : bool {
45
                    return $change->isRemoved();
46
                },
47
                ...$arrayOfChanges
48
            ))
49
        );
50
    }
51
52
    private function convertFilteredChangesToMarkdownBulletList(callable $filterFunction, Change ...$changes) : array
53
    {
54
        return array_map(
55
            function (Change $change) : string {
56
                return ' - ' . str_replace(['ADDED: ', 'CHANGED: ', 'REMOVED: '], '', trim((string)$change)) . "\n";
57
            },
58
            array_filter($changes, $filterFunction)
59
        );
60
    }
61
}
62