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 ( 94978a...f8e0da )
by Andy
03:07
created

MarathonJobComparisonBusinessCase::isEqual()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 7.756
c 0
b 0
f 0
cc 9
eloc 8
nc 4
nop 2
crap 9
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\MarathonAppEntity;
18
use Chapi\Service\JobRepository\JobRepositoryInterface;
19
20
class MarathonJobComparisonBusinessCase extends AbstractJobComparisionBusinessCase
21
{
22
    /**
23
     * @param JobRepositoryInterface $localRepository
24
     * @param JobRepositoryInterface $remoteRepository
25
     * @param DiffCompareInterface $diffCompare
26
     */
27 24
    public function __construct(
28
        JobRepositoryInterface $localRepository,
29
        JobRepositoryInterface $remoteRepository,
30
        DiffCompareInterface $diffCompare
31
    ) {
32 24
        $this->remoteRepository = $remoteRepository;
33 24
        $this->localRepository = $localRepository;
34 24
        $this->diffCompare = $diffCompare;
35 24
    }
36
37 4
    protected function preCompareModifications(JobEntityInterface &$localJob, JobEntityInterface &$remoteJob)
38
    {
39 4
        if (!$localJob instanceof MarathonAppEntity ||
40 4
            !$remoteJob instanceof MarathonAppEntity
41
        ) {
42
            throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.');
43
        }
44
        // marathon returns portDefinitions values for auto configured port as well
45
        // we want to only check if the port is defined in local file.
46
        // otherwise we ignore the remote values.
47 4
        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...
48 4
            $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...
49
        }
50
51 4
        if ($localJob->container && $localJob->container->docker &&
52 4
            $remoteJob->container && $remoteJob->container->docker) {
53
54 1
            foreach ($localJob->container->docker->portMappings as $index => $localPortMapping) {
55 1
                if ($localPortMapping->servicePort !== 0) {
56
                    continue;
57
                }
58
59 1
                if (!isset($remoteJob->container->docker->portMappings, $index)) {
60
                    continue;
61
                }
62
63 1
                $remotePortMapping = $remoteJob->container->docker->portMappings[$index];
64
65 1
                if ($remotePortMapping != $localPortMapping) {
66 1
                    $fixedPortMapping = clone $remotePortMapping;
67 1
                    $fixedPortMapping->servicePort = 0;
68
69 1
                    if ($fixedPortMapping == $localPortMapping) {
70 1
                        unset($localJob->container->docker->portMappings[$index]);
71 1
                        unset($remoteJob->container->docker->portMappings[$index]);
72
                    }
73
                }
74
            }
75
        }
76 4
    }
77
78
    /**
79
     * @return JobEntityInterface
80
     */
81
    protected function getEntitySetWithDefaults()
82
    {
83
        return new MarathonAppEntity();
84
    }
85
86
    /**
87
     * @param JobEntityInterface|ChronosJobEntity $jobEntityA
88
     * @param JobEntityInterface|ChronosJobEntity $jobEntityB
89
     * @return bool
90
     */
91
    public function hasSameJobType(JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB)
92
    {
93
        // for now we don't have a concrete seperation
94
        // of types for marathon.
95
        return true;
96
    }
97
98
    /**
99
     * @param $property
100
     * @param $jobEntityA
101
     * @param $jobEntityB
102
     * @return bool
103
     */
104 18
    protected function isEntityEqual($property, JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB)
105
    {
106 18
        if (!$jobEntityA instanceof MarathonAppEntity ||
107 18
            !$jobEntityB instanceof MarathonAppEntity
108
        ) {
109
            throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.');
110
        }
111
112 18
        return $this->isEqual($jobEntityA->{$property}, $jobEntityB->{$property});
113
    }
114
115
    /**
116
     * @param mixed $valueA
117
     * @param mixed $valueB
118
     * @return bool
119
     */
120 19
    private function isEqual($valueA, $valueB)
121
    {
122 19
        if (is_array($valueA) && is_array($valueB)) {
123 7
            return $this->isArrayEqual($valueA, $valueB);
124 18
        } elseif (is_object($valueA) && is_object($valueB)) {
125 8
            return $this->isArrayEqual(get_object_vars($valueA), get_object_vars($valueB));
126 16
        } elseif ((is_scalar($valueA) && is_scalar($valueB)) || (is_null($valueA) && is_null($valueB))) {
127 12
            return $valueA == $valueB;
128
        }
129
130 5
        return false;
131
    }
132
133
    /**
134
     * @param array $valuesA
135
     * @param array $valuesB
136
     * @return bool
137
     */
138 12
    private function isArrayEqual(array $valuesA, array $valuesB)
139
    {
140 12
        return $this->isArrayHalfEqual($valuesA, $valuesB) && $this->isArrayHalfEqual($valuesB, $valuesA);
141
    }
142
143
    /**
144
     * @param array $valuesA
145
     * @param array $valuesB
146
     * @return bool
147
     */
148 12
    private function isArrayHalfEqual(array $valuesA, array $valuesB)
149
    {
150 12
        foreach ($valuesA as $keyA => $valueA) {
151 11
            if (is_string($keyA)) {
152 8
                if (!array_key_exists($keyA, $valuesB) || !$this->isEqual($valueA, $valuesB[$keyA])) {
153 8
                    return false;
154
                }
155
            } else {
156 6
                foreach ($valuesB as $valueB) {
157 6
                    if ($this->isEqual($valueA, $valueB)) {
158 6
                        continue 2;
159
                    }
160
                }
161
162 8
                return false;
163
            }
164
        }
165
166 9
        return true;
167
    }
168
}
169