GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b3f485...e9fc06 )
by Andy
13s
created

DiffCommand::process()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
ccs 0
cts 13
cp 0
rs 6.7272
cc 7
eloc 14
nc 6
nop 0
crap 56
1
<?php
2
/**
3
 * @package: chapi
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2015-07-30
7
 *
8
 */
9
10
namespace Chapi\Commands;
11
12
use Chapi\BusinessCase\Comparison\JobComparisonInterface;
13
use Chapi\Service\JobRepository\JobRepository;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputOption;
16
17
class DiffCommand extends AbstractCommand
18
{
19
    /**
20
     * Configures the current command.
21
     */
22
    protected function configure()
23
    {
24
        $this->setName('diff')
25
            ->setDescription('Show changes between jobs and working tree, etc')
26
            ->addArgument('jobName', InputArgument::OPTIONAL, 'Show changes for specific job')
27
            ->addOption(
28
                'strict',
29
                null,
30
                InputOption::VALUE_NONE,
31
                "Return a non-zero exit code when there are changes",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Return a non-zero exit code when there are changes does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
32
                null
33
            );
34
    }
35
36
    /**
37
     * @return int
38
     */
39
    protected function process()
40
    {
41
        /** @var JobComparisonInterface  $jobComparisonBusinessCase */
42
        $jobComparisonBusinessCase = $this->getContainer()->get(JobComparisonInterface::DIC_NAME);
43
        $jobName = $this->input->getArgument('jobName');
44
45
        $changed = false;
46
47
        if (!empty($jobName)) {
48
            $changed = $this->printJobDiff($jobName);
49
        } else {
50
            $localJobUpdates = $jobComparisonBusinessCase->getLocalJobUpdates();
51
            if (!empty($localJobUpdates)) {
52
                foreach ($localJobUpdates as $jobName) {
53
                    $changed = $changed || $this->printJobDiff($jobName);
54
                }
55
            }
56
        }
57
58
        if ($this->input->getOption('strict') && $changed) {
59
            return 1;
60
        }
61
62
        return 0;
63
    }
64
65
    /**
66
     * @param string $jobName
67
     */
68
    private function printJobDiff($jobName)
69
    {
70
        /** @var JobComparisonInterface  $jobComparisonBusinessCase */
71
        $jobComparisonBusinessCase = $this->getContainer()->get(JobComparisonInterface::DIC_NAME);
72
73
        $jobs = [ $jobName ];
74
75
        if (strpos($jobName, '*') !== false) {
76
            $jobs = $this->getJobsMatchingWildcard($jobName);
77
        }
78
79
        $changed = false;
80
81
        foreach ($jobs as $jobName) {
82
            $changed = $changed || $this->printSingleJobDiff($jobComparisonBusinessCase, $jobName);
83
        }
84
85
        return $changed;
86
    }
87
88
    /**
89
     * @param JobComparisonInterface $jobComparisonBusinessCase
90
     * @param string $jobName
91
     */
92
    private function printSingleJobDiff(JobComparisonInterface $jobComparisonBusinessCase, $jobName)
93
    {
94
        $this->output->writeln(sprintf("\n<comment>diff %s</comment>", $jobName));
95
96
        $jobDiff = $jobComparisonBusinessCase->getJobDiff($jobName);
97
98
        foreach ($jobDiff as $property => $diff) {
99
            $diffLines = explode(PHP_EOL, $diff);
100
101
            // the first line might be missing some leading whitespace
102
            if (count($diffLines) > 1) {
103
                $lastLine = $diffLines[count($diffLines) - 1];
104
105
                if (strpos($lastLine, ' ') === 0) {
106
                    $length = strspn($lastLine, ' ');
107
108
                    $diffLines[0] = substr($lastLine, 0, $length) . $diffLines[0];
109
                }
110
            }
111
112
            foreach ($diffLines as $diffLine) {
113
                $diffSign = substr($diffLine, 0, 1);
114
115
                if ($diffSign == '+') {
116
                    $this->output->writeln(sprintf("<info>%s\t%s: %s</info>", $diffSign, $property, ' ' . substr($diffLine, 1)));
117
                } elseif ($diffSign == '-') {
118
                    $this->output->writeln(sprintf("<fg=red>%s\t%s: %s</>", $diffSign, $property, ' ' . substr($diffLine, 1)));
119
                } else {
120
                    $this->output->writeln(sprintf(" \t%s: %s", $property, $diffLine));
121
                }
122
            }
123
        }
124
125
        $this->output->writeln("\n");
126
127
        return !empty($jobDiff);
128
    }
129
130
    /**
131
     * @param string $jobName
132
     * @return string[]
133
     */
134
    private function getJobsMatchingWildcard($jobName)
135
    {
136
        /** @var JobRepository[] $jobRepositories */
137
        $jobRepositories = [
138
            $this->getContainer()->get(JobRepository::DIC_NAME_CHRONOS),
139
            $this->getContainer()->get(JobRepository::DIC_NAME_FILESYSTEM_CHRONOS),
140
            $this->getContainer()->get(JobRepository::DIC_NAME_FILESYSTEM_MARATHON),
141
            $this->getContainer()->get(JobRepository::DIC_NAME_MARATHON)
142
        ];
143
144
        $jobNames = [];
145
146
        foreach ($jobRepositories as $jobRepository) {
147
            foreach ($jobRepository->getJobs() as $job) {
148
                if (fnmatch($jobName, $job->getKey())) {
149
                    $jobNames[$job->getKey()] = true;
150
                }
151
            }
152
        }
153
154
        ksort($jobNames);
155
156
        return array_keys($jobNames);
157
    }
158
}
159