Completed
Push — master ( 9be290...7a90a5 )
by Gilmar
23:47
created

AbstractDecorator::toArray()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
ccs 6
cts 6
cp 1
cc 2
eloc 7
nc 3
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of gpupo/netshoes-sdk
5
 * Created by Gilmar Pupo <[email protected]>
6
 * For the information of copyright and license you should read the file
7
 * LICENSE which is distributed with this source code.
8
 * Para a informação dos direitos autorais e de licença você deve ler o arquivo
9
 * LICENSE que é distribuído com este código-fonte.
10
 * Para obtener la información de los derechos de autor y la licencia debe leer
11
 * el archivo LICENSE que se distribuye con el código fuente.
12
 * For more information, see <http://www.g1mr.com/>.
13
 */
14
15
namespace Gpupo\NetshoesSdk\Entity\Order\Decorator;
16
17
use Gpupo\Common\Entity\Collection;
18
use Gpupo\CommonSdk\Traits\LoggerTrait;
19
use Gpupo\NetshoesSdk\Entity\Order\Order;
20
21
abstract class AbstractDecorator extends Collection
22
{
23
    use LoggerTrait;
24
25
    protected $name = '';
26
27 5
    protected function fail($string = '')
28
    {
29 5
        $message = 'Order incomplete for status ['.$string.']';
30 5
        $this->log('error', $message, [
31 5
            'order' => $this->getOrder(),
32
        ]);
33
34 5
        throw new \InvalidArgumentException($message);
35
    }
36
37 5
    protected function invalid($string = '')
38
    {
39 5
        $message = 'Attribute invalid: '.$string.' ';
40
41 5
        throw new \InvalidArgumentException($message);
42
    }
43
44 10
    public function setOrder(Order $order)
45
    {
46 10
        $this->set('order', $order);
47 10
        $this->initLogger($order->getLogger());
48
49 10
        return $this;
50
    }
51
52 5
    public function getOrder()
53
    {
54 5
        return $this->get('order');
55
    }
56
57 14
    public function validate()
58
    {
59 14
        $order = $this->getOrder();
60
61 14
        if (empty($order)) {
62 4
            $this->invalid('Order');
63
        }
64
65 10
        $this->getOrder()->check();
66
67 10
        return $this;
68
    }
69
70 15
    public function toArray()
71
    {
72
        try {
73 15
            $this->validate();
74 10
            $array = $this->factoryArray();
0 ignored issues
show
Documentation Bug introduced by
The method factoryArray does not exist on object<Gpupo\NetshoesSdk...ator\AbstractDecorator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
75 5
        } catch (\Exception $e) {
76 5
            $this->fail($this->name . ' ('.$e->getMessage().')');
77
        }
78
79 10
        return $array;
0 ignored issues
show
Bug introduced by
The variable $array does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
80
    }
81
}
82