Completed
Pull Request — master (#473)
by Dimitris
01:20
created

CallTimesPrediction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 16.85 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 15
loc 89
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A getTimes() 0 3 1
A check() 15 53 4

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
/*
4
 * This file is part of the Prophecy.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *     Marcello Duarte <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Prophecy\Prediction;
13
14
use Prophecy\Call\Call;
15
use Prophecy\Prophecy\ObjectProphecy;
16
use Prophecy\Prophecy\MethodProphecy;
17
use Prophecy\Argument\ArgumentsWildcard;
18
use Prophecy\Argument\Token\AnyValuesToken;
19
use Prophecy\Util\StringUtil;
20
use Prophecy\Exception\Prediction\UnexpectedCallsCountException;
21
22
/**
23
 * Prediction interface.
24
 * Predictions are logical test blocks, tied to `should...` keyword.
25
 *
26
 * @author Konstantin Kudryashov <[email protected]>
27
 */
28
class CallTimesPrediction implements PredictionInterface
29
{
30
    private $times;
31
    private $util;
32
33
    /**
34
     * Initializes prediction.
35
     *
36
     * @param int        $times
37
     * @param StringUtil $util
38
     */
39
    public function __construct($times, StringUtil $util = null)
40
    {
41
        $this->times = intval($times);
42
        $this->util  = $util ?: new StringUtil;
43
    }
44
45
    /**
46
     * Returns the prediction's times.
47
     *
48
     * @return int $times
49
     */
50
    public function getTimes() {
51
        return $this->times;
52
    }
53
54
    /**
55
     * Tests that there was exact amount of calls made.
56
     *
57
     * @param Call[]         $calls
58
     * @param ObjectProphecy $object
59
     * @param MethodProphecy $method
60
     *
61
     * @throws \Prophecy\Exception\Prediction\UnexpectedCallsCountException
62
     */
63
    public function check(array $calls, ObjectProphecy $object, MethodProphecy $method)
64
    {
65
        if ($this->times == count($calls)) {
66
            return;
67
        }
68
69
        $methodCalls = $object->findProphecyMethodCalls(
70
            $method->getMethodName(),
71
            new ArgumentsWildcard(array(new AnyValuesToken))
72
        );
73
74
        if (count($calls)) {
75
            $message = sprintf(
76
                "Expected exactly %d calls that match:\n".
77
                "  %s->%s(%s)\n".
78
                "but %d were made:\n%s",
79
80
                $this->times,
81
                get_class($object->reveal()),
82
                $method->getMethodName(),
83
                $method->getArgumentsWildcard(),
84
                count($calls),
85
                $this->util->stringifyCalls($calls)
86
            );
87 View Code Duplication
        } elseif (count($methodCalls)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
88
            $message = sprintf(
89
                "Expected exactly %d calls that match:\n".
90
                "  %s->%s(%s)\n".
91
                "but none were made.\n".
92
                "Recorded `%s(...)` calls:\n%s",
93
94
                $this->times,
95
                get_class($object->reveal()),
96
                $method->getMethodName(),
97
                $method->getArgumentsWildcard(),
98
                $method->getMethodName(),
99
                $this->util->stringifyCalls($methodCalls)
100
            );
101
        } else {
102
            $message = sprintf(
103
                "Expected exactly %d calls that match:\n".
104
                "  %s->%s(%s)\n".
105
                "but none were made.",
106
107
                $this->times,
108
                get_class($object->reveal()),
109
                $method->getMethodName(),
110
                $method->getArgumentsWildcard()
111
            );
112
        }
113
114
        throw new UnexpectedCallsCountException($message, $method, $this->times, $calls);
115
    }
116
}
117