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
Pull Request — master (#94)
by Patrick
02:10
created

DiffCommand::printJobDiff()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
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
16
class DiffCommand extends AbstractCommand
17
{
18
    /**
19
     * Configures the current command.
20
     */
21
    protected function configure()
22
    {
23
        $this->setName('diff')
24
            ->setDescription('Show changes between jobs and working tree, etc')
25
            ->addArgument('jobName', InputArgument::OPTIONAL, 'Show changes for specific job')
26
        ;
27
    }
28
29
    /**
30
     * @return int
31
     */
32
    protected function process()
33
    {
34
        /** @var JobComparisonInterface  $jobComparisonBusinessCase */
35
        $jobComparisonBusinessCase = $this->getContainer()->get(JobComparisonInterface::DIC_NAME);
36
        $jobName = $this->input->getArgument('jobName');
37
38
        if (!empty($jobName)) {
39
            $this->printJobDiff($jobName);
40
        } else {
41
            $localJobUpdates = $jobComparisonBusinessCase->getLocalJobUpdates();
42
            if (!empty($localJobUpdates)) {
43
                foreach ($localJobUpdates as $jobName) {
44
                    $this->printJobDiff($jobName);
45
                }
46
            }
47
        }
48
49
        return 0;
50
    }
51
52
    /**
53
     * @param string $jobName
54
     */
55
    private function printJobDiff($jobName)
56
    {
57
        /** @var JobComparisonInterface  $jobComparisonBusinessCase */
58
        $jobComparisonBusinessCase = $this->getContainer()->get(JobComparisonInterface::DIC_NAME);
59
60
        $jobs = [ $jobName ];
61
62
        if (strpos($jobName, '*') !== false) {
63
            $jobs = $this->getJobsMatchingWildcard($jobName);
64
            var_dump($jobs);die;
0 ignored issues
show
Security Debugging Code introduced by
var_dump($jobs); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
Coding Style Compatibility introduced by
The method printJobDiff() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
65
        }
66
67
        foreach ($jobs as $jobName) {
68
            $this->printSingleJobDiff($jobComparisonBusinessCase, $jobName);
69
        }
70
    }
71
72
    /**
73
     * @param JobComparisonInterface $jobComparisonBusinessCase
74
     * @param string $jobName
75
     */
76
    private function printSingleJobDiff(JobComparisonInterface $jobComparisonBusinessCase, $jobName)
77
    {
78
        $this->output->writeln(sprintf("\n<comment>diff %s</comment>", $jobName));
79
80
        $jobDiff = $jobComparisonBusinessCase->getJobDiff($jobName);
81
82
        foreach ($jobDiff as $property => $diff) {
83
            $diffLines = array_reverse(explode(PHP_EOL, $diff));
84
85
            foreach ($diffLines as $diffLine) {
86
                $diffSign = substr($diffLine, 0, 1);
87
88
                if ($diffSign == '+') {
89
                    $this->output->writeln(sprintf("<info>%s\t%s: %s</info>", $diffSign, $property, substr($diffLine, 1)));
90
                } elseif ($diffSign == '-') {
91
                    $this->output->writeln(sprintf("<fg=red>%s\t%s: %s</>", $diffSign, $property, substr($diffLine, 1)));
92
                } else {
93
                    $this->output->writeln(sprintf("\t%s: %s", $property, $diffLine));
94
                }
95
            }
96
        }
97
98
        $this->output->writeln("\n");
99
    }
100
101
    /**
102
     * @param string $jobName
103
     * @return string[]
104
     */
105
    private function getJobsMatchingWildcard($jobName)
106
    {
107
        /** @var JobRepository[] $jobRepositories */
108
        $jobRepositories = [
109
            $this->getContainer()->get(JobRepository::DIC_NAME_CHRONOS),
110
            $this->getContainer()->get(JobRepository::DIC_NAME_FILESYSTEM_CHRONOS),
111
            $this->getContainer()->get(JobRepository::DIC_NAME_FILESYSTEM_MARATHON),
112
            $this->getContainer()->get(JobRepository::DIC_NAME_MARATHON)
113
        ];
114
115
        $jobNames = [];
116
117
        foreach ($jobRepositories as $jobRepository) {
118
            foreach ($jobRepository->getJobs() as $job) {
119
                if (fnmatch($jobName, $job->getKey())) {
120
                    $jobNames[] = $job->getKey();
121
                }
122
            }
123
        }
124
125
        sort($jobNames);
126
127
        return array_values(array_unique($jobNames));
128
    }
129
}
130