ResultCollection   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 13.79 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 16
loc 116
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3
ccs 32
cts 32
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findByPhone() 0 13 3
A getSuccessfullyResults() 8 8 1
A getFailedResults() 8 8 1
A current() 0 4 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A count() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the SmsGate package
7
 *
8
 * (c) SmsGate
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code
12
 */
13
14
namespace SmsGate;
15
16
use SmsGate\Exception\ResultNotFoundException;
17
18
/**
19
 * The collection for store results of send sms.
20
 *
21
 * @author Vitaliy Zhuk <[email protected]>
22
 */
23
class ResultCollection implements \Iterator, \Countable
24
{
25
    /**
26
     * @var Result[]
27
     */
28
    private $results;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param Result[] ...$results
34
     */
35 6
    public function __construct(Result ...$results)
36
    {
37 6
        $this->results = $results;
0 ignored issues
show
Documentation Bug introduced by
It seems like $results of type array<integer,array<inte...bject<SmsGate\Result>>> is incompatible with the declared type array<integer,object<SmsGate\Result>> of property $results.

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...
38 6
    }
39
40
    /**
41
     * Find the result by phone
42
     *
43
     * @param Phone $phone
44
     *
45
     * @return Result
46
     *
47
     * @throws ResultNotFoundException
48
     */
49 2
    public function findByPhone(Phone $phone): Result
50
    {
51 2
        foreach ($this->results as $result) {
52 2
            if ($result->getPhone()->getValue() === $phone->getValue()) {
53 1
                return $result;
54
            }
55
        }
56
57 1
        throw new ResultNotFoundException(sprintf(
58 1
            'Not found result for phone "%s".',
59 1
            $phone
60
        ));
61
    }
62
63
    /**
64
     * Get successfully results
65
     *
66
     * @return ResultCollection
67
     */
68 View Code Duplication
    public function getSuccessfullyResults(): ResultCollection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70 1
        $successfullyResults = array_filter($this->results, function (Result $result) {
71 1
            return $result->isSuccessfully();
72 1
        });
73
74 1
        return new self(...$successfullyResults);
75
    }
76
77
    /**
78
     * Get failed results
79
     *
80
     * @return ResultCollection
81
     */
82 View Code Duplication
    public function getFailedResults(): ResultCollection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84 1
        $failedResults = array_filter($this->results, function (Result $result) {
85 1
            return $result->isFailed();
86 1
        });
87
88 1
        return new self(...$failedResults);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 2
    public function current(): Result
95
    {
96 2
        return current($this->results);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 2
    public function next(): void
103
    {
104 2
        next($this->results);
105 2
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 1
    public function key()
111
    {
112 1
        return key($this->results);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 2
    public function valid(): bool
119
    {
120 2
        return key($this->results) !== null;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 2
    public function rewind(): void
127
    {
128 2
        reset($this->results);
129 2
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 3
    public function count(): int
135
    {
136 3
        return count($this->results);
137
    }
138
}
139