DiffCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Copyright (c) Andreas Heigl<[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 *
24
 * @author    Andreas Heigl<[email protected]>
25
 * @copyright Andreas Heigl
26
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
27
 * @since     14.06.2016
28
 * @link      http://github.com/heiglandreas/org.heigl.junitdiff
29
 */
30
31
namespace Org_Heigl\JUnitDiff\Command;
32
33
use Org_Heigl\JUnitDiff\JUnitParser;
34
use Symfony\Component\Console\Command\Command;
35
use Symfony\Component\Console\Input\InputInterface;
36
use Symfony\Component\Console\Input\InputOption;
37
use Symfony\Component\Console\Output\OutputInterface;
38
39
class DiffCommand extends Command
40
{
41
    protected function configure()
42
    {
43
        $this->setName('diff')
44
             ->setDescription('Shows a diff between two JUnit log files')
45
             ->setDefinition([
46
                 new InputOption('input1', '1', InputOption::VALUE_REQUIRED, 'First input file'),
47
                 new InputOption('input2', '2', InputOption::VALUE_REQUIRED, 'Second input file')
48
             ])
49
             ->setHelp('');
50
    }
51
52
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $output->writeln([
0 ignored issues
show
Documentation introduced by
array($this->getApplicat...->getLongVersion(), '') is of type array<integer,?,{"0":"?","1":"string"}>, but the function expects a string|object<Symfony\Co...onsole\Output\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
            $this->getApplication()->getLongVersion(),
57
            '',
58
        ]);
59
60
        $parser = new JUnitParser();
61
62
        try {
63
            $file1  = $input->getOption('input1');
64
            $array1 = $parser->parseFile($file1);
65
        } catch (\Exception $e) {
66
            $output->writeln('<bg=red;fg=white>  ' . $e->getMessage() . '  </>');
67
            return;
68
        }
69
70
        try {
71
            $file2  = $input->getOption('input2');
72
            $array2 = $parser->parseFile($file2);
73
        } catch (\Exception $e) {
74
            $output->writeln('<bg=red;fg=white> ' . $e->getMessage() . '  </>');
75
            return;
76
        }
77
78
        $array = $this->merge($array1, $array2);
79
80
        foreach ($array as $key => $value) {
81
            if (! isset($value['base'])) {
82
                $output->writeln('<bg=green;fg=black>+</> ' . $key);
83
                continue;
84
            }
85
            if (! isset($value['current'])) {
86
                $output->writeln('<bg=red;fg=yellow>-</> ' . $key);
87
                continue;
88
            }
89
90
            if ($value['base']['result'] != $value['current']['result']) {
91
                $output->writeln(sprintf(
92
                    '<bg=blue;fg=yellow>o</> %s changed from <fg=cyan>%s</> to <fg=magenta>%s</>',
93
                    $key,
94
                    $array1[$key]['result'],
95
                    $array2[$key]['result']
96
                ));
97
                continue;
98
            }
99
        }
100
101
        $output->writeln('');
102
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
103
            $output->writeln([
0 ignored issues
show
Documentation introduced by
array(sprintf('<bg=green...', $file1, $file2), '') is of type array<integer,string,{"0..."string","3":"string"}>, but the function expects a string|object<Symfony\Co...onsole\Output\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
                sprintf(
105
                    '<bg=green;fg=black>+</>: This test was added in file %s',
106
                    $file2
107
                ),
108
                sprintf(
109
                    '<bg=red;fg=yellow>-</>: This test was removed in file %s',
110
                    $file2
111
                ),
112
                sprintf(
113
                    '<bg=blue;fg=yellow>o</>: The test-result changed between file %s and %s',
114
                    $file1,
115
                    $file2
116
                ),
117
                ''
118
            ]);
119
        }
120
        $output->writeln(sprintf(
121
            '<fg=yellow>Analyzed %s tests in total, %s tests in file %s and %s tests in file %s',
122
            count($array),
123
            count($array1),
124
            basename($file1),
125
            count($array2),
126
            basename($file2)
127
        ));
128
    }
129
130
    protected function merge(array $array1, array $array2)
131
    {
132
        $merged = [];
133
        foreach ($array1 as $key => $value) {
134
            $merged[$key]['base'] = $value;
135
        }
136
137
        foreach ($array2 as $key => $value) {
138
            $merged[$key]['current'] = $value;
139
        }
140
141
        ksort($merged);
142
143
        return $merged;
144
    }
145
}
146