Completed
Push — master ( 44b975...9348fd )
by Jakub
01:41
created

PhpAttributesEngine   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 17
c 1
b 0
f 1
dl 0
loc 39
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isAvailable() 0 2 1
A getReflection() 0 7 4
A hasAnnotation() 0 5 2
A getClassName() 0 2 1
A getAnnotation() 0 9 3
1
<?php
2
declare(strict_types=1);
3
4
namespace MyTester\Annotations;
5
6
use Nette\Utils\Strings;
7
use ReflectionClass;
8
use ReflectionException;
9
use ReflectionMethod;
10
use Reflector;
11
12
/**
13
 * Php attributes engine for annotations reader
14
 *
15
 * @author Jakub Konečný
16
 * @internal
17
 */
18
final class PhpAttributesEngine implements \MyTester\IAnnotationsReaderEngine {
19
  public function hasAnnotation(string $name, $class, string $method = null): bool {
20
    if(!$this->isAvailable()) {
21
      return false;
22
    }
23
    return count($this->getReflection($class, $method)->getAttributes($this->getClassName($name))) > 0;
1 ignored issue
show
introduced by
The method getAttributes() does not exist on ReflectionMethod. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
    return count($this->getReflection($class, $method)->/** @scrutinizer ignore-call */ getAttributes($this->getClassName($name))) > 0;
Loading history...
Bug introduced by
The method getAttributes() does not exist on ReflectionClass. It seems like you code against a sub-type of ReflectionClass such as Nette\Reflection\ClassType. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
    return count($this->getReflection($class, $method)->/** @scrutinizer ignore-call */ getAttributes($this->getClassName($name))) > 0;
Loading history...
24
  }
25
26
  public function getAnnotation(string $name, $class, string $method = null) {
27
    if(!$this->isAvailable()) {
28
      return null;
29
    }
30
    $attributes = $this->getReflection($class, $method)->getAttributes($this->getClassName($name));
31
    if(count($attributes) === 0) {
32
      return null;
33
    }
34
    return $attributes[0]->getArguments()[0] ?? null;
35
  }
36
37
  private function isAvailable(): bool {
38
    return version_compare(PHP_VERSION, "7.5.0", ">");
39
  }
40
41
  private function getClassName(string $baseName): string {
42
    return "MyTester\\Annotations\Attributes\\" . Strings::firstUpper($baseName);
43
  }
44
45
  /**
46
   * @param string|object $class
47
   * @return ReflectionClass|ReflectionMethod
48
   * @throws ReflectionException
49
   */
50
  private function getReflection($class, string $method = null): Reflector {
51
    if($method !== null) {
52
      $reflection = new ReflectionMethod(is_object($class) ? get_class($class) : $class, $method);
53
    } else {
54
      $reflection = new ReflectionClass(is_object($class) ? get_class($class) : $class);
55
    }
56
    return $reflection;
57
  }
58
}
59
?>