Completed
Push — master ( 98a82f...7bd861 )
by Márcio Lucas R.
11s
created

FuncoesReflections   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 0
loc 229
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A pegaNomesMetodosClasse() 0 4 1
A pegaNomeAtributoEspecifico() 0 23 3
A pegaAtributosDoObjeto() 0 18 2
A pegaNomeClasseObjeto() 0 6 1
A retornaValoresAtributosClassesMaes() 0 20 3
A verificaSeEClasseFilha() 0 10 2
A retornaClassesMaes() 0 17 3
A pegaValoresAtributoDoObjeto() 0 16 2
A retornaNomeAtributosClassesMaes() 0 14 2
A retornaComentariosAtributos() 0 19 2
1
<?php
2
3
/**
4
 * Copyright (c) 2017. Este código foi feito por @marciioluucas, sob licença MIT
5
 */
6
namespace Phiber\Util;
7
8
use Exception;
9
use Phiber\ORM\Exceptions\NotImplementedException;
10
use ReflectionClass;
11
use ReflectionProperty;
12
13
/**
14
 *
15
 * Classe responsável por Fazer a reflexao das classes dos objetos.
16
 * @package util
17
 */
18
class FuncoesReflections
19
{
20
    /**
21
     * Função responsável por pegar o nome dos métodos do objeto retornando um array dos mesmos
22
     * 
23
     * @param  $object
24
     * @return array
25
     */
26
    public function pegaNomesMetodosClasse($object)
27
    {
28
        return get_class_methods($object);
29
    }
30
31
    /**
32
     * Função responsável por pegar o nome de um atributo espefífico.
33
     * Caso o atributo pesquisado não exista, a função retornará falso.
34
     * 
35
     * @param  $object
36
     * @param  $nomeAtributo
37
     * @return bool|string
38
     * @throws Exception
39
     */
40
    public function pegaNomeAtributoEspecifico($object, $nomeAtributo)
41
    {
42
        try {
43
            $arrayAtributosObjeto = self::pegaAtributosDoObjeto($object);
44
            
45
            $iterator = 0;
46
            $limit    = count($arrayAtributosObjeto);
47
            for ($iterator; $iterator < $limit; $iterator++) {
0 ignored issues
show
Unused Code introduced by
$iterator++; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
48
                
49
                $atributoEspecifico = strstr(
50
                    $arrayAtributosObjeto[$iterator], 
51
                    $nomeAtributo
52
                );
53
                
54
                return $atributoEspecifico;
55
            }
56
57
        } catch (Exception $e) {
58
            throw new Exception("Falha ao pegar nome do atributo específico", 3, $e);
59
        }
60
61
        return false;
62
    }
63
64
    /**
65
     * Função responsável por pegar os nomes dos atributos do objeto, retornando um array dos mesmos.
66
     * 
67
     * @param $obj
68
     * @return array
69
     */
70
    public function pegaAtributosDoObjeto($obj)
71
    {
72
        $properties      = [];
73
        $reflectionClass = new ReflectionClass($obj);
74
        $propriedades    = $reflectionClass->getProperties(
75
            ReflectionProperty::IS_PUBLIC |
76
            ReflectionProperty::IS_PROTECTED | 
77
            ReflectionProperty::IS_PRIVATE
78
        );
79
     
80
        $iterator = 0;
0 ignored issues
show
Unused Code introduced by
$iterator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
        $limit    = count($propriedades);
82
        for ($iterator = 0; $iterator < $limit; $iterator++) {
83
            $properties[$iterator] = $propriedades[$iterator]->name;
84
        }
85
86
        return $properties;
87
    }
88
89
    /**
90
     * Função responsável por pegar o nome da classe do objeto em questão.
91
     * 
92
     * @param  $object
93
     * @return string
94
     */
95
    public function pegaNomeClasseObjeto($object)
96
    {
97
        $reflectionClass = new ReflectionClass($object);
98
99
        return $reflectionClass->getShortName();
100
    }
101
102
    /**
103
     * Função responsável por retornar os valores dos atributos das classes mães,
104
     * se as mesmas existirem, se caso a classe em questão não for uma classe filha, 
105
     * a função retornará false.
106
     * 
107
     * @param  $object
108
     * @return bool|array
109
     */
110
    public function retornaValoresAtributosClassesMaes($object)
111
    {
112
        if (self::verificaSeEClasseFilha($object)) {
113
            
114
            $nomeClassesMae = self::retornaClassesMaes($object);
115
            
116
            $valores  = [];
117
            $iterator = 0;
118
            $limit    = count($nomeClassesMae);
119
            for ($iterator; $iterator < $limit; $iterator++) {
120
                $valores[$iterator] = self::pegaValoresAtributoDoObjeto(
121
                    $nomeClassesMae[$iterator]
122
                );
123
            }
124
125
            return $valores;
126
        }
127
128
        return false;
129
    }
130
131
    /**
132
     * Função responsável por verifidar se a classe é filha de alguma outra classe,
133
     * se caso não for. A função retornará falso.
134
     * 
135
     * @param  Object $object
136
     * @return bool
137
     */
138
    public function verificaSeEClasseFilha($object)
139
    {
140
        $class = new ReflectionClass($object);
141
142
        if ($class->getParentClass()) {
143
            return true;
144
        }
145
        
146
        return false;
147
    }
148
149
    /**
150
     * Retorna o nome das classes mães, se caso o objeto em questão não ter uma classe mãe,
151
     * a função retornará false.
152
     * 
153
     * @param  $object
154
     * @return array|bool
155
     */
156
    public function retornaClassesMaes($object)
157
    {
158
        $class = new ReflectionClass($object);
159
160
        $parents = [];
161
        $parent  = "";
162
        if ( self::verificaSeEClasseFilha($object) ) {
163
            while ($class->getParentClass()) {
164
                $parents[] = $class->getParentClass()->getName();
165
                $class     = $parent;
166
            }
167
168
            return $parents;
169
        }
170
171
        return false;
172
    }
173
174
    /**
175
     * Função responsável por retornar um array de todos os valores dos atributos de um objeto
176
     * 
177
     * @param $object
178
     * @return array
179
     */
180
    public function pegaValoresAtributoDoObjeto($object)
181
    {
182
        $nomeAtributos   = self::pegaAtributosDoObjeto($object);
183
        $valAtrFinal     = [];
184
        $reflectionClass = new ReflectionClass($object);
185
        
186
        $iterator = 0;
187
        $limit    = count($nomeAtributos);
188
        for ($iterator; $iterator < $limit; $iterator++) {
189
            $reflectionProperty = $reflectionClass->getProperty($nomeAtributos[$iterator]);
190
            $reflectionProperty->setAccessible(true);
191
            $valAtrFinal[$iterator] = $reflectionProperty->getValue($object);
192
        }
193
194
        return $valAtrFinal;
195
    }
196
197
198
    /**
199
     * Função responsável por retornar um array com todos os atributos das classes da hierarquia
200
     * 
201
     * @todos Pendente de implementação
202
     * 
203
     * @param $obj
204
     * @return array
205
     */
206
    public function retornaNomeAtributosClassesMaes($obj)
0 ignored issues
show
Unused Code introduced by
The parameter $obj 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...
207
    {
208
        throw new NotImplementedException();
209
210
        $atributos = [];
0 ignored issues
show
Unused Code introduced by
$atributos = array(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
211
        $iterator  = 0;
212
        $limit     = count(self::retornaClassesMaes($obj));
213
        for ($iterator; $iterator < $limit; $iterator++) {
214
            $atributos[$iterator] = array(self::retornaClassesMaes($obj)[$iterator] =>
215
                self::pegaAtributosDoObjeto(self::retornaClassesMaes($obj)[$iterator]));
216
        }
217
218
        return $atributos;
219
    }
220
221
    /**
222
     * Função responsável por retornar os comentários dos atributos do objeto em questão.
223
     * 
224
     * @param  $object
225
     * @return array
226
     */
227
    public function retornaComentariosAtributos($object)
228
    {
229
        $arrAttrNames = self::pegaAtributosDoObjeto($object);
230
        $arrAttrComm  = array();
231
        
232
        $iterator = 0;
233
        $limit    = count($arrAttrNames);
234
        for ($iterator; $iterator < $limit; $iterator++) {
235
236
            $reflectionAttr = new ReflectionProperty($object, $arrAttrNames[$iterator]);
237
238
            $arrAttrComm[$arrAttrNames[$iterator]] = $reflectionAttr->getDocComment();
239
        }
240
241
        /**
242
         * @todo FAZER O FOR PARA PEGAR TODOS ATRIBUTOS, PASSAR NO SEGUNDO PARAMETRO;
243
         */
244
        return $arrAttrComm;
245
    }
246
}
247