Completed
Pull Request — master (#18)
by Stéphane
66:12 queued 61:24
created

ExecutionInfos::__get()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 6
nc 3
nop 1
1
<?php
2
/**
3
 * This file is part of the bee4/transport package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2015
8
 * @author  Stephane HULARD <[email protected]>
9
 * @package Bee4\Transport\Handle
10
 */
11
12
namespace Bee4\Transport\Handle;
13
14
/**
15
 * Handle execution informations implementation
16
 * @package Bee4\Transport\Handle
17
 */
18
class ExecutionInfos
19
{
20
    /**
21
     * Handle used to perform the execution
22
     */
23
    private $handle;
24
25
    /**
26
     * Execution status code
27
     * @var integer
28
     */
29
    private $status;
0 ignored issues
show
Unused Code introduced by
The property $status is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
31
    /**
32
     * Header collection
33
     * @var string
34
     */
35
    private $headers;
0 ignored issues
show
Unused Code introduced by
The property $headers is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
36
37
    /**
38
     * Effective URL
39
     * @var string
40
     */
41
    private $effectiveUrl;
0 ignored issues
show
Unused Code introduced by
The property $effectiveUrl is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
42
43
    /**
44
     * Transaction time
45
     * @var float
46
     */
47
    private $transactionTime;
0 ignored issues
show
Unused Code introduced by
The property $transactionTime is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
48
49
    /**
50
     * Content type for the request
51
     * @var string
52
     */
53
    private $contentType;
0 ignored issues
show
Unused Code introduced by
The property $contentType is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
54
55
    /**
56
     * Build an ExecutioInfos instance
57
     * @param HandleInterface $handle
58
     */
59
    public function __construct(
60
        HandleInterface $handle,
61
        callable $resolver = null
0 ignored issues
show
Unused Code introduced by
The parameter $resolver is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    ) {
63
        $this->handle = $handle;
64
    }
65
66
    /**
67
     * Retrieve current handle
68
     * @return HandleInterface
69
     */
70
    public function getHandle()
71
    {
72
        return $this->handle;
73
    }
74
75
    /**
76
     * Retrieve a property value
77
     * @param  string $name Property name
78
     * @return mixed
79
     */
80
    public function __get($name)
81
    {
82
        if (!property_exists($this, $name)) {
83
            throw new \BadMethodCallException('Invalid property name: '.$name);
84
        }
85
86
        if (null === $this->$name && isset($this->resolver)) {
87
            $this->$name = call_user_func($this->resolver, $name);
0 ignored issues
show
Documentation introduced by
The property resolver does not exist on object<Bee4\Transport\Handle\ExecutionInfos>. 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...
88
        }
89
90
        return $this->$name;
91
    }
92
93
    /**
94
     * Set a property
95
     * @param  string $name
96
     * @param  array  $arguments
97
     * @return ExecutionInfos
98
     */
99
    public function __call($name, array $arguments)
100
    {
101
        //Define getter and setter for each valid property
102
        if (property_exists($this, $name) && count($arguments) == 1) {
103
            $this->$name = $arguments[0];
104
            return $this;
105
        } else {
106
            throw new \BadMethodCallException('Invalid method: '.$name);
107
        }
108
    }
109
}
110