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

ProxyTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 86
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __moka_getMock() 0 4 1
A __moka_setMock() 0 6 1
A __moka_setMockingStrategy() 0 6 1
A stub() 0 7 1
A doCall() 0 10 2
A doGet() 0 8 2
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 Alberto Villa
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 Alberto Villa
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