Completed
Push — master ( 7ee214...32fd8d )
by Alberto
19s
created

ProxyTrait::__moka_setMockingStrategy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Proxy;
5
6
use Moka\Exception\InvalidArgumentException;
7
use Moka\Exception\MockNotCreatedException;
8
use Moka\Strategy\MockingStrategyInterface;
9
10
/**
11
 * Trait ProxyTrait
12
 * @package Moka\Proxy
13
 */
14
trait ProxyTrait
15
{
16
    /**
17
     * @var object
18
     */
19
    private $__moka_mock;
20
21
    /**
22
     * @var MockingStrategyInterface
23
     */
24
    private $__moka_mockingStrategy;
25
26
    /**
27
     * @return object
28
     */
29 4
    public function __moka_getMock()
30
    {
31 4
        return $this->__moka_mock;
32
    }
33
34
    /**
35
     * @param object $mock
36
     * @return ProxyInterface|ProxyTrait
37
     */
38 16
    public function __moka_setMock($mock): self
39
    {
40 16
        $this->__moka_mock = $mock;
41
42 16
        return $this;
43
    }
44
45
    /**
46
     * @param MockingStrategyInterface $mockingStrategy
47
     * @return ProxyInterface|ProxyTrait
48
     */
49 14
    public function __moka_setMockingStrategy(MockingStrategyInterface $mockingStrategy): self
50
    {
51 14
        $this->__moka_mockingStrategy = $mockingStrategy;
52
53 14
        return $this;
54
    }
55
56
    /**
57
     * @param array $namesWithValues
58
     * @return ProxyInterface
59
     *
60
     * @throws InvalidArgumentException
61
     * @throws MockNotCreatedException
62
     */
63 1
    public function stub(array $namesWithValues): ProxyInterface
64
    {
65
        /** @var $this ProxyInterface */
66 1
        $this->__moka_mockingStrategy->decorate($this->__moka_mock, $namesWithValues);
2 ignored issues
show
Bug introduced by
Accessing __moka_mockingStrategy on the interface Moka\Proxy\ProxyInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing __moka_mock on the interface Moka\Proxy\ProxyInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
67
68 1
        return $this;
69
    }
70
71
    /**
72
     * @param string $name
73
     * @param array $arguments
74
     * @return mixed
75
     */
76 6
    protected function doCall(string $name, array $arguments)
77
    {
78 6
        if (!$this->__moka_mockingStrategy instanceof MockingStrategyInterface) {
79 1
            return null;
80
        }
81
82 5
        $target = $this->__moka_mockingStrategy->get($this->__moka_mock);
83
84 5
        return $target->$name(...$arguments);
85
    }
86
87
    /**
88
     * @param string $name
89
     * @return mixed
90
     */
91 3
    protected function doGet(string $name)
92
    {
93 3
        if (!$this->__moka_mockingStrategy instanceof MockingStrategyInterface) {
94 1
            return null;
95
        }
96
97 2
        return $this->__moka_mockingStrategy->call($this->__moka_mock, $name);
98
    }
99
}
100