Test Setup Failed
Push — master ( c03d3f...88f4a8 )
by Carsten
03:55
created

ApplySalesman::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace Germania\Salesmen;
3
4
class ApplySalesman
5
{
6
7
    /**
8
     * @var Callable
9
     */
10
    public $salesman_factory;
11
12
    /**
13
     * @var SalesmanInterface
14
     */
15
    public $salesman_fallback;
16
17
18
    /**
19
     * @param callable $salesman_factory Callable that takes a Salesman ID that should return a SalesmanInterface instance.
20
     */
21
    public function __construct( callable $salesman_factory, SalesmanInterface $salesman_fallback = null )
22
    {
23
        $this->salesman_factory  = $salesman_factory;
24
        $this->salesman_fallback = $salesman_fallback;
25
    }
26
27
28
    /**
29
     * @param  object $aggregator Object that
30
     * @return bool
31
     */
32
    public function __invoke( $aggregator )
33
    {
34
35
        // Default value
36
        if (!isset($aggregator->salesman)):
37
            $aggregator->salesman = null;
38
        endif;
39
40
41
        // Prepare using
42
        $salesman_factory = $this->salesman_factory;
43
44
45
        // If salesman_id is available
46
        if ($aggregator instanceOf SalesmanIdProviderInterface):
47
            $aggregator->salesman = $salesman_factory( $aggregator->getSalesmanId(), $this->salesman_fallback );
0 ignored issues
show
Bug introduced by
Accessing salesman on the interface Germania\Salesmen\SalesmanIdProviderInterface 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...
48
49
        elseif (isset($aggregator->salesman_id)):
50
            $aggregator->salesman = $salesman_factory( $aggregator->salesman_id, $this->salesman_fallback );
51
52
        // Salesman can not be set
53
        else:
54
            return false;
55
        endif;
56
57
        return true;
58
    }
59
}
60