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.

MarathonJobComparisonBusinessCase   B
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 39
lcom 1
cbo 6
dl 0
loc 175
ccs 60
cts 70
cp 0.8571
rs 8.2857
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
C preCompareModifications() 0 66 15
A getEntitySetWithDefaults() 0 4 1
A hasSameJobType() 0 6 1
A isEntityEqual() 0 10 3
B isEqual() 0 12 9
A isArrayEqual() 0 4 2
B isArrayHalfEqual() 0 20 7
1
<?php
2
/**
3
 *
4
 * @package: chapi
5
 *
6
 * @author: bthapaliya
7
 * @since: 2016-12-14
8
 *
9
 */
10
11
namespace Chapi\BusinessCase\Comparison;
12
13
use Chapi\Component\Comparison\DiffCompareInterface;
14
use Chapi\Entity\Chronos\ChronosJobEntity;
15
use Chapi\Entity\JobEntityInterface;
16
use Chapi\Entity\Marathon\AppEntity\DockerPortMapping;
17
use Chapi\Entity\Marathon\AppEntity\Fetch;
18
use Chapi\Entity\Marathon\AppEntity\Network;
19
use Chapi\Entity\Marathon\MarathonAppEntity;
20
use Chapi\Service\JobRepository\JobRepositoryInterface;
21
22
class MarathonJobComparisonBusinessCase extends AbstractJobComparisionBusinessCase
23
{
24
    /**
25
     * @param JobRepositoryInterface $localRepository
26
     * @param JobRepositoryInterface $remoteRepository
27
     * @param DiffCompareInterface $diffCompare
28
     */
29 25
    public function __construct(
30
        JobRepositoryInterface $localRepository,
31
        JobRepositoryInterface $remoteRepository,
32
        DiffCompareInterface $diffCompare
33
    ) {
34 25
        $this->remoteRepository = $remoteRepository;
35 25
        $this->localRepository = $localRepository;
36 25
        $this->diffCompare = $diffCompare;
37 25
    }
38
39 5
    protected function preCompareModifications(JobEntityInterface &$localJob, JobEntityInterface &$remoteJob)
40
    {
41 5
        if (!$localJob instanceof MarathonAppEntity ||
42 5
            !$remoteJob instanceof MarathonAppEntity
43
        ) {
44
            throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.');
45
        }
46
        // marathon returns portDefinitions values for auto configured port as well
47
        // we want to only check if the port is defined in local file.
48
        // otherwise we ignore the remote values.
49 5
        if (!$localJob->portDefinitions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $localJob->portDefinitions of type Chapi\Entity\Marathon\AppEntity\PortDefinition[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
50 5
            $remoteJob->portDefinitions = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array<integer,object<Cha...Entity\PortDefinition>> of property $portDefinitions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
        }
52
53
        // convert uris to fetchers
54 5
        foreach ($localJob->uris as $uri) {
55
            $localJob->fetch[] = new Fetch(["uri" => $uri, "extract" => true]);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal uri 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...
Coding Style Comprehensibility introduced by
The string literal extract 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...
56
        }
57 5
        $localJob->uris = [];
58 5
        foreach ($remoteJob->uris as $uri) {
59
            $remoteJob->fetch[] = new Fetch(["uri" => $uri, "extract" => true]);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal uri 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...
Coding Style Comprehensibility introduced by
The string literal extract 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...
60
        }
61 5
        $remoteJob->uris = [];
62
63 5
        if ($localJob->container && $remoteJob->container) {
64
65 2
            $localPortMappings = $localJob->container->portMappings;
66 2
            $remotePortMappings = $remoteJob->container->portMappings;
67
68 2
            usort($localPortMappings, DockerPortMapping::class . '::less');
69 2
            usort($remotePortMappings, DockerPortMapping::class . '::less');
70
71 2
            foreach ($localPortMappings as $index => $localPortMapping) {
72 1
                if ($localPortMapping->servicePort !== 0) {
73
                    continue;
74
                }
75
76 1
                if (!isset($remotePortMappings[$index])) {
77
                    continue;
78
                }
79
80 1
                $remotePortMapping = $remotePortMappings[$index];
81
82 1
                if (DockerPortMapping::less($remotePortMapping, $localPortMapping) != 0) {
83 1
                    $fixedPortMapping = clone $remotePortMapping;
84 1
                    $fixedPortMapping->servicePort = 0;
85
86 1
                    if (DockerPortMapping::less($fixedPortMapping, $localPortMapping) == 0) {
87 1
                        unset($localPortMappings[$index]);
88 1
                        unset($remotePortMappings[$index]);
89
                    }
90
                }
91
            }
92
93 2
            $localJob->container->portMappings = array_values($localPortMappings);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values($localPortMappings) of type array<integer,?> is incompatible with the declared type array<integer,object<Cha...ity\DockerPortMapping>> of property $portMappings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
94 2
            $remoteJob->container->portMappings = array_values($remotePortMappings);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_values($remotePortMappings) of type array<integer,?> is incompatible with the declared type array<integer,object<Cha...ity\DockerPortMapping>> of property $portMappings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
95
        }
96
97
        // set network to "host" when not containerized
98 5
        if (!$localJob->container) {
99 3
            $localJob->networks = [new Network(["mode" => "host"])];
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal mode 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...
Coding Style Comprehensibility introduced by
The string literal host 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...
100
        }
101 5
        if (!$remoteJob->container) {
102 3
            $remoteJob->networks = [new Network(["mode" => "host"])];
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal mode 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...
Coding Style Comprehensibility introduced by
The string literal host 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...
103
        }
104 5
    }
105
106
    /**
107
     * @return JobEntityInterface
108
     */
109
    protected function getEntitySetWithDefaults()
110
    {
111
        return new MarathonAppEntity();
112
    }
113
114
    /**
115
     * @param JobEntityInterface|ChronosJobEntity $jobEntityA
116
     * @param JobEntityInterface|ChronosJobEntity $jobEntityB
117
     * @return bool
118
     */
119
    public function hasSameJobType(JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB)
120
    {
121
        // for now we don't have a concrete seperation
122
        // of types for marathon.
123
        return true;
124
    }
125
126
    /**
127
     * @param $property
128
     * @param $jobEntityA
129
     * @param $jobEntityB
130
     * @return bool
131
     */
132 19
    protected function isEntityEqual($property, JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB)
133
    {
134 19
        if (!$jobEntityA instanceof MarathonAppEntity ||
135 19
            !$jobEntityB instanceof MarathonAppEntity
136
        ) {
137
            throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.');
138
        }
139
140 19
        return $this->isEqual($jobEntityA->{$property}, $jobEntityB->{$property});
141
    }
142
143
    /**
144
     * @param mixed $valueA
145
     * @param mixed $valueB
146
     * @return bool
147
     */
148 20
    private function isEqual($valueA, $valueB)
149
    {
150 20
        if (is_array($valueA) && is_array($valueB)) {
151 8
            return $this->isArrayEqual($valueA, $valueB);
152 19
        } elseif (is_object($valueA) && is_object($valueB)) {
153 9
            return $this->isArrayEqual(get_object_vars($valueA), get_object_vars($valueB));
154 17
        } elseif ((is_scalar($valueA) && is_scalar($valueB)) || (is_null($valueA) && is_null($valueB))) {
155 13
            return $valueA == $valueB;
156
        }
157
158 5
        return false;
159
    }
160
161
    /**
162
     * @param array $valuesA
163
     * @param array $valuesB
164
     * @return bool
165
     */
166 13
    private function isArrayEqual(array $valuesA, array $valuesB)
167
    {
168 13
        return $this->isArrayHalfEqual($valuesA, $valuesB) && $this->isArrayHalfEqual($valuesB, $valuesA);
169
    }
170
171
    /**
172
     * @param array $valuesA
173
     * @param array $valuesB
174
     * @return bool
175
     */
176 13
    private function isArrayHalfEqual(array $valuesA, array $valuesB)
177
    {
178 13
        foreach ($valuesA as $keyA => $valueA) {
179 12
            if (is_string($keyA)) {
180 9
                if (!array_key_exists($keyA, $valuesB) || !$this->isEqual($valueA, $valuesB[$keyA])) {
181 9
                    return false;
182
                }
183
            } else {
184 6
                foreach ($valuesB as $valueB) {
185 6
                    if ($this->isEqual($valueA, $valueB)) {
186 6
                        continue 2;
187
                    }
188
                }
189
190 9
                return false;
191
            }
192
        }
193
194 10
        return true;
195
    }
196
}
197