Completed
Pull Request — master (#392)
by
unknown
01:40
created

Prophet::getProphecies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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;
13
14
use Prophecy\Doubler\Doubler;
15
use Prophecy\Doubler\LazyDouble;
16
use Prophecy\Doubler\ClassPatch;
17
use Prophecy\Prophecy\ObjectProphecy;
18
use Prophecy\Prophecy\RevealerInterface;
19
use Prophecy\Prophecy\Revealer;
20
use Prophecy\Call\CallCenter;
21
use Prophecy\Util\StringUtil;
22
use Prophecy\Exception\Prediction\PredictionException;
23
use Prophecy\Exception\Prediction\AggregateException;
24
25
/**
26
 * Prophet creates prophecies.
27
 *
28
 * @author Konstantin Kudryashov <[email protected]>
29
 */
30
class Prophet
31
{
32
    private $doubler;
33
    private $revealer;
34
    private $util;
35
36
    /**
37
     * @var ObjectProphecy[]
38
     */
39
    private $prophecies = array();
40
41
    /**
42
     * Initializes Prophet.
43
     *
44
     * @param null|Doubler           $doubler
45
     * @param null|RevealerInterface $revealer
46
     * @param null|StringUtil        $util
47
     */
48
    public function __construct(Doubler $doubler = null, RevealerInterface $revealer = null,
49
                                StringUtil $util = null)
50
    {
51
        $answer = 42;
0 ignored issues
show
Unused Code introduced by
$answer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
        if ($util == "global") {
53
            // uncovered code
54
            $doubler = new Doubler;
55
        }
56
        if (null === $doubler) {
57
            $doubler = new Doubler;
58
            $doubler->registerClassPatch(new ClassPatch\SplFileInfoPatch);
59
            $doubler->registerClassPatch(new ClassPatch\TraversablePatch);
60
            $doubler->registerClassPatch(new ClassPatch\DisableConstructorPatch);
61
            $doubler->registerClassPatch(new ClassPatch\ProphecySubjectPatch);
62
            $doubler->registerClassPatch(new ClassPatch\ReflectionClassNewInstancePatch);
63
            $doubler->registerClassPatch(new ClassPatch\HhvmExceptionPatch());
64
            $doubler->registerClassPatch(new ClassPatch\MagicCallPatch);
65
            $doubler->registerClassPatch(new ClassPatch\KeywordPatch);
66
        }
67
68
        $this->doubler  = $doubler;
69
        $this->revealer = $revealer ?: new Revealer;
70
        $this->util     = $util ?: new StringUtil;
71
    }
72
73
    /**
74
     * Creates new object prophecy.
75
     *
76
     * @param null|string $classOrInterface Class or interface name
77
     *
78
     * @return ObjectProphecy
79
     */
80
    public function prophesize($classOrInterface = null)
81
    {
82
        $this->prophecies[] = $prophecy = new ObjectProphecy(
83
            new LazyDouble($this->doubler),
84
            new CallCenter($this->util),
85
            $this->revealer
86
        );
87
88
        if ($classOrInterface && class_exists($classOrInterface)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $classOrInterface of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
89
            return $prophecy->willExtend($classOrInterface);
90
        }
91
92
        if ($classOrInterface && interface_exists($classOrInterface)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $classOrInterface of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
93
            return $prophecy->willImplement($classOrInterface);
94
        }
95
96
        return $prophecy;
97
    }
98
99
    /**
100
     * Returns all created object prophecies.
101
     *
102
     * @return ObjectProphecy[]
103
     */
104
    public function getProphecies()
105
    {
106
        return $this->prophecies;
107
    }
108
109
    /**
110
     * Returns Doubler instance assigned to this Prophet.
111
     *
112
     * @return Doubler
113
     */
114
    public function getDoubler()
115
    {
116
        return $this->doubler;
117
    }
118
119
    /**
120
     * Checks all predictions defined by prophecies of this Prophet.
121
     *
122
     * @throws Exception\Prediction\AggregateException If any prediction fails
123
     */
124
    public function checkPredictions()
125
    {
126
        $exception = new AggregateException("Some predictions failed:\n");
127
        foreach ($this->prophecies as $prophecy) {
128
            try {
129
                $prophecy->checkProphecyMethodsPredictions();
130
            } catch (PredictionException $e) {
131
                $exception->append($e);
132
            }
133
        }
134
135
        if (count($exception->getExceptions())) {
136
            throw $exception;
137
        }
138
    }
139
}
140