Electricity::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 15
cts 15
cp 1
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Dalen\OWLPacketInterceptor\Packet\Electricity;
4
5
use Dalen\OWLPacketInterceptor\Packet\Base;
6
use Dalen\OWLPacketInterceptor\Packet\IPacket;
7
use Dalen\OWLPacketInterceptor\Packet\Collection;
8
9
class Electricity extends Base implements IPacket
10
{
11
    
12 7
    function __construct($data = NULL)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
    {
14 7
        $this->fields = array
15
        (
16 7
            'id'        => '',
17 7
            'signal'    => NULL,
18 7
            'battery'   => NULL,
19 7
            'channels'  => new Collection(),
20
        );
21
        
22 7
        if(!empty($data))
23 7
        {
24 6
            parent::__construct(array('id' => $data['id']));
25
            
26 6
            if(is_array($data))
27 6
            {
28 6
                $this->loadFromArray($data);
29 6
            }
30 6
        }
31 7
    }
32
    
33
    /*
34
     * @parameter $data Array
35
     */
36 6
    private function loadFromArray($data)
37
    {
38 6
        foreach($data['chan'] AS $channel)
39
        {
40 6
            $this->fields['channels']->addItem(new Channel($channel));
41 6
        }
42
43 6
        $this->fields['signal']     =  new Signal($data['signal']);
44 6
        $this->fields['battery']    =  new Battery($data['battery']);
45 6
    }
46
    
47
    /*
48
     * returns the Domain's type
49
     */
50 1
    function getType()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
    {
52 1
        return get_class();
53
    }
54
55
    /*
56
     * returns the object id
57
     */
58 1
    public function getId()
59
    {
60 1
        return $this->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Dalen\OWLPacketIn...lectricity\Electricity>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
61
    }
62
63
    /*
64
     * returns the Json representation of the object
65
     */
66 1
    public function toJson()
67
    {
68 1
        return json_encode($this);
69
    }
70
71
}