Completed
Push — master ( dfda39...6a8025 )
by Uni
06:40
created

ExplicitGetter::__internalGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * @author         UniKado <[email protected]>
4
 * @copyright  (c) 2016, UniKado
5
 * @package        UK\DynamicProperties
6
 * @since          2016-04-01
7
 * @version        0.2.0
8
 */
9
10
11
declare( strict_types = 1 );
12
13
14
namespace UK\DynamicProperties;
15
16
17
/**
18
 * If you extend you're class from this class, you're class becomes an automatic working magic __get method for
19
 * read access the return values of all available getXyz methods by dynamic properties.
20
 *
21
 * For example: If you're class defines the two public get methods 'getFoo()' and 'getFooBar()' you can also
22
 * access the resulting return values with the dynamic properties $instance->foo and $instance->fooBar.
23
 *
24
 * But do'nt forget to document you're dyn. class properties with the @ property-read annotation
25
 *
26
 * @since v0.1.0-dev (Tata43)
27
 */
28
abstract class ExplicitGetter
29
{
30
31
32
   // <editor-fold desc="// = = = =   P R O T E C T E D   F I E L D S   = = = = = = = = = = = = = = = = = = = = = = =">
33
34
   /**
35
    * The names of all get* methods (without the leading get) that should not be mapped as dynamic properties
36
    *
37
    * @type  array
38
    * @since v0.2.0
39
    */
40
   protected $ignoreGetProperties = [];
41
42
   // </editor-fold>
43
44
45
   // <editor-fold desc="// = = = =   P U B L I C   M E T H O D S   = = = = = = = = = = = = = = = = = = = = = = = = =">
46
47
   /**
48
    * The magic get method to let you access all getter methods by dynamic properties.
49
    *
50
    * @param  string $name The name of the required dynamic property.
51
    * @return mixed
52
    * @throws \LogicException If the property is unknown
53
    */
54 14
   public function __get( string $name )
55
   {
56
57 14
      return $this->__internalGet( $name );
58
59
   }
60
61
   /**
62
    * Magic isset implementation.
63
    *
64
    * @param  string $name The name of the required property.
65
    * @return boolean
66
    */
67 6
   public function __isset( string $name )
68
   {
69
70 6
      return $this->hasReadableProperty( $name, $getterName );
71
72
   }
73
74
   /**
75
    * Returns, if a property with the defined name exists for read access.
76
    *
77
    * @param  string $name       The name of the property.
78
    * @param  string $getterName Returns the name of the associated get method, if method returns TRUE.
79
    * @return boolean
80
    */
81 7 View Code Duplication
   public function hasReadableProperty( string $name, &$getterName ) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
   {
83
84 7
      if ( \in_array( $name, $this->ignoreGetProperties ) )
85
      {
86 1
         return false;
87
      }
88
89 6
      $getterName = 'get' . \ucfirst( $name );
90
91 6
      return \method_exists( $this, $getterName );
92
93
   }
94
95
   // </editor-fold>
96
97
98
   // <editor-fold desc="// = = = =   P R O T E C T E D   M E T H O D S   = = = = = = = = = = = = = = = = = = = = = =">
99
100
   /**
101
    * @param string $name
102
    * @return mixed
103
    */
104 14
   protected function __internalGet( string $name )
105
   {
106
107
      // The name of the required getMethod
108 14
      if ( ! $this->hasReadableProperty( $name, $getterName ) )
109
      {
110 2
         throw new \LogicException( 'No such property: ' . __CLASS__ . '::$' . $name. '!' );
111
      }
112
113 12
      return $this->$getterName();
114
115
   }
116
117
   // </editor-fold>
118
119
120
}
121
122