Completed
Push — master ( f05361...a0ad3b )
by David
02:06 queued 11s
created

CacheCommandTrait::setupOutputStyle()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Command;
13
14
use Imagine\Exception\RuntimeException;
15
use Liip\ImagineBundle\Component\Console\Style\ImagineStyle;
16
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
17
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * @internal
23
 */
24
trait CacheCommandTrait
25
{
26
    /**
27
     * @var CacheManager
28
     */
29
    private $cacheManager;
30
31
    /**
32
     * @var FilterManager
33
     */
34
    private $filterManager;
35
36
    /**
37
     * @var ImagineStyle
38
     */
39
    private $io;
40
41
    /**
42
     * @var bool
43
     */
44
    private $outputMachineReadable;
45
46
    /**
47
     * @var int
48
     */
49
    private $failures = 0;
50
51
    private function setupOutputStyle(InputInterface $input, OutputInterface $output): void
52
    {
53
        $this->outputMachineReadable = $input->getOption('as-script');
0 ignored issues
show
Documentation Bug introduced by
It seems like $input->getOption('as-script') can also be of type string or array<integer,string>. However, the property $outputMachineReadable is declared as type boolean. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
54
        $this->io = new ImagineStyle($input, $output, $this->outputMachineReadable ? false : !$input->getOption('no-colors'));
55
    }
56
57
    /**
58
     * @return array[]
59
     */
60
    private function resolveInputFiltersAndPaths(InputInterface $input): array
61
    {
62
        return [
63
            $input->getArgument('path'),
64
            $this->normalizeFilterList($input->getOption('filter')),
65
        ];
66
    }
67
68
    /**
69
     * @param string[] $filters
70
     *
71
     * @return string[]
72
     */
73
    private function normalizeFilterList(array $filters): array
74
    {
75
        if (0 < count($filters)) {
76
            return $filters;
77
        }
78
79
        if (0 < count($filters = array_keys((array) $this->filterManager->getFilterConfiguration()->all()))) {
80
            return $filters;
81
        }
82
83
        throw new RuntimeException('No filters have been defined in the active configuration!');
84
    }
85
86
    private function outputCommandHeader(): void
87
    {
88
        if (!$this->outputMachineReadable) {
89
            $this->io->title($this->getName(), 'liip/imagine-bundle');
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
90
        }
91
    }
92
93
    /**
94
     * @param string[] $images
95
     * @param string[] $filters
96
     */
97
    private function outputCommandResult(array $images, array $filters, string $singularAction): void
98
    {
99
        if (!$this->outputMachineReadable) {
100
            $wordPluralizer = function (int $count, string $singular) {
101
                return 1 === $count ? $singular : sprintf('%ss', $singular);
102
            };
103
            $rootTextFormat = 'Completed %d %s (%d %s, %d %s)';
104
            $imagePathsSize = count($images);
105
            $filterSetsSize = count($filters);
106
            $allActionsSize = ($filterSetsSize * $imagePathsSize) - $this->failures;
107
            $allActionsWord = $wordPluralizer($allActionsSize, $singularAction);
108
109
            $rootTextOutput = vsprintf($rootTextFormat, [
110
                $allActionsSize,
111
                $allActionsWord,
112
                $imagePathsSize,
113
                $wordPluralizer($imagePathsSize, 'image'),
114
                $filterSetsSize,
115
                $wordPluralizer($filterSetsSize, 'filter'),
116
            ]);
117
118
            if ($this->failures) {
119
                $this->io->critBlock(sprintf('%s %%s', $rootTextOutput), [
120
                    sprintf('[encountered %d failures]', $this->failures)
121
                ]);
122
            } else {
123
                $this->io->okayBlock($rootTextOutput);
124
            }
125
        }
126
    }
127
128
    private function getResultCode(): int
129
    {
130
        return 0 === $this->failures ? 0 : 255;
131
    }
132
}
133