ExactValueToken::scoreArgument()   C
last analyzed

Complexity

Conditions 13
Paths 11

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 6.6166
c 0
b 0
f 0
cc 13
nc 11
nop 1

How to fix   Complexity   

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\Argument\Token;
13
14
use SebastianBergmann\Comparator\ComparisonFailure;
15
use Prophecy\Comparator\Factory as ComparatorFactory;
16
use Prophecy\Util\StringUtil;
17
18
/**
19
 * Exact value token.
20
 *
21
 * @author Konstantin Kudryashov <[email protected]>
22
 */
23
class ExactValueToken implements TokenInterface
24
{
25
    private $value;
26
    private $string;
27
    private $util;
28
    private $comparatorFactory;
29
30
    /**
31
     * Initializes token.
32
     *
33
     * @param mixed             $value
34
     * @param StringUtil        $util
35
     * @param ComparatorFactory $comparatorFactory
36
     */
37 View Code Duplication
    public function __construct($value, StringUtil $util = null, ComparatorFactory $comparatorFactory = null)
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...
38
    {
39
        $this->value = $value;
40
        $this->util  = $util ?: new StringUtil();
41
42
        $this->comparatorFactory = $comparatorFactory ?: ComparatorFactory::getInstance();
43
    }
44
45
    /**
46
     * Scores 10 if argument matches preset value.
47
     *
48
     * @param $argument
49
     *
50
     * @return bool|int
51
     */
52
    public function scoreArgument($argument)
53
    {
54
        if (is_object($argument) && is_object($this->value)) {
55
            $comparator = $this->comparatorFactory->getComparatorFor(
56
                $argument, $this->value
57
            );
58
59
            try {
60
                $comparator->assertEquals($argument, $this->value);
61
                return 10;
62
            } catch (ComparisonFailure $failure) {
63
            	return false;
64
			}
65
        }
66
67
        // If either one is an object it should be castable to a string
68
        if (is_object($argument) xor is_object($this->value)) {
69
            if (is_object($argument) && !method_exists($argument, '__toString')) {
70
                return false;
71
            }
72
73
            if (is_object($this->value) && !method_exists($this->value, '__toString')) {
74
                return false;
75
            }
76
        } elseif (is_numeric($argument) && is_numeric($this->value)) {
77
            // noop
78
        } elseif (gettype($argument) !== gettype($this->value)) {
79
            return false;
80
        }
81
82
        return $argument == $this->value ? 10 : false;
83
    }
84
85
    /**
86
     * Returns preset value against which token checks arguments.
87
     *
88
     * @return mixed
89
     */
90
    public function getValue()
91
    {
92
        return $this->value;
93
    }
94
95
    /**
96
     * Returns false.
97
     *
98
     * @return bool
99
     */
100
    public function isLast()
101
    {
102
        return false;
103
    }
104
105
    /**
106
     * Returns string representation for token.
107
     *
108
     * @return string
109
     */
110 View Code Duplication
    public function __toString()
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...
111
    {
112
        if (null === $this->string) {
113
            $this->string = sprintf('exact(%s)', $this->util->stringify($this->value));
114
        }
115
116
        return $this->string;
117
    }
118
}
119