Completed
Push — master ( 2f35f3...5b14ac )
by Rafal
10:52 queued 06:57
created

Points::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 19
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
4
namespace App\GameBetting\Business\GameExtraPoints;
5
6
7
use App\GameBetting\Business\GameExtraPoints\Score\ScoreInterface;
8
use App\GameBetting\Persistence\DataProvider\ExtraResult;
9
10
class Points implements PointsInterface
11
{
12
    /**
13
     * @var ScoreInterface
14
     */
15
    private $collections;
16
17
    /**
18
     * @param ScoreInterface[] $collections
19
     */
20
    public function __construct(...$collections)
21
    {
22
        $this->collections = $collections;
0 ignored issues
show
Documentation Bug introduced by
It seems like $collections of type array<integer,App\GameBe...s\Score\ScoreInterface> is incompatible with the declared type App\GameBetting\Business...ts\Score\ScoreInterface of property $collections.

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...
23
    }
24
25
    /**
26
     * @param ExtraResult $extraResult
27
     * @return int
28
     */
29
    public function get(ExtraResult $extraResult): int
30
    {
31
        $this->checkCollection();
32
33
        usort($this->collections, function ($a, $b) {
0 ignored issues
show
Bug introduced by
$this->collections of type App\GameBetting\Business...ts\Score\ScoreInterface is incompatible with the type array expected by parameter $array of usort(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        usort(/** @scrutinizer ignore-type */ $this->collections, function ($a, $b) {
Loading history...
34
            return $a->getScore() <=> $b->getScore();
35
        });
36
        $this->collections = array_reverse($this->collections);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_reverse($this->collections) of type array is incompatible with the declared type App\GameBetting\Business...ts\Score\ScoreInterface of property $collections.

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...
37
38
        $score = 0;
39
40
        foreach ($this->collections as $collection) {
41
            if ($collection->check($extraResult) === true) {
42
                $score = $collection->getScore();
43
                break;
44
            }
45
        }
46
47
        return $score;
48
    }
49
50
51
    private function checkCollection()
52
    {
53
        foreach ($this->collections as $collection) {
54
            if (!$collection instanceof ScoreInterface) {
55
                throw new \RuntimeException('Collection: ' . get_class($collection) . 'is not instanceof ' . ScoreInterface::class);
56
            }
57
        }
58
    }
59
60
61
}