CallTimesPrediction::check()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 53

Duplication

Lines 15
Ratio 28.3 %

Importance

Changes 0
Metric Value
dl 15
loc 53
rs 9.0254
c 0
b 0
f 0
cc 4
nc 4
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     * Tests that there was exact amount of calls made.
47
     *
48
     * @param Call[]         $calls
49
     * @param ObjectProphecy $object
50
     * @param MethodProphecy $method
51
     *
52
     * @throws \Prophecy\Exception\Prediction\UnexpectedCallsCountException
53
     */
54
    public function check(array $calls, ObjectProphecy $object, MethodProphecy $method)
55
    {
56
        if ($this->times == count($calls)) {
57
            return;
58
        }
59
60
        $methodCalls = $object->findProphecyMethodCalls(
61
            $method->getMethodName(),
62
            new ArgumentsWildcard(array(new AnyValuesToken))
63
        );
64
65
        if (count($calls)) {
66
            $message = sprintf(
67
                "Expected exactly %d calls that match:\n".
68
                "  %s->%s(%s)\n".
69
                "but %d were made:\n%s",
70
71
                $this->times,
72
                get_class($object->reveal()),
73
                $method->getMethodName(),
74
                $method->getArgumentsWildcard(),
75
                count($calls),
76
                $this->util->stringifyCalls($calls)
77
            );
78 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...
79
            $message = sprintf(
80
                "Expected exactly %d calls that match:\n".
81
                "  %s->%s(%s)\n".
82
                "but none were made.\n".
83
                "Recorded `%s(...)` calls:\n%s",
84
85
                $this->times,
86
                get_class($object->reveal()),
87
                $method->getMethodName(),
88
                $method->getArgumentsWildcard(),
89
                $method->getMethodName(),
90
                $this->util->stringifyCalls($methodCalls)
91
            );
92
        } else {
93
            $message = sprintf(
94
                "Expected exactly %d calls that match:\n".
95
                "  %s->%s(%s)\n".
96
                "but none were made.",
97
98
                $this->times,
99
                get_class($object->reveal()),
100
                $method->getMethodName(),
101
                $method->getArgumentsWildcard()
102
            );
103
        }
104
105
        throw new UnexpectedCallsCountException($message, $method, $this->times, $calls);
106
    }
107
}
108