ApplySalesman   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 57
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 28 4
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  SalesmanIdProviderInterface|StdClass $aggregator Object that provides a salesman_id
30
     * @return bool
31
     */
32
    public function __invoke( $aggregator )
33
    {
34
35
36
        // Prepare callable
37
        $salesman_factory = $this->salesman_factory;
38
39
40
        // Add salesman proporty if not exists
41
        if (!isset($aggregator->salesman)):
42
            $aggregator->salesman = null;
43
        endif;
44
45
46
        // If salesman_id is available
47
        if ($aggregator instanceOf SalesmanIdProviderInterface):
48
            $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...
49
50
        elseif (isset($aggregator->salesman_id)):
51
            $aggregator->salesman = $salesman_factory( $aggregator->salesman_id, $this->salesman_fallback );
52
53
        // Salesman can not be set
54
        else:
55
            return false;
56
        endif;
57
58
        return true;
59
    }
60
}
61