Template::onMethodInject()   F
last analyzed

Complexity

Conditions 21
Paths 544

Size

Total Lines 71
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 25.5311

Importance

Changes 0
Metric Value
eloc 47
c 0
b 0
f 0
dl 0
loc 71
ccs 36
cts 46
cp 0.7826
rs 0.6333
cc 21
nc 544
nop 3
crap 25.5311

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace WebStream\Annotation\Attributes;
4
5
use WebStream\Annotation\Base\Annotation;
6
use WebStream\Annotation\Base\IAnnotatable;
7
use WebStream\Annotation\Base\IMethod;
8
use WebStream\Annotation\Base\IRead;
9
use WebStream\Container\Container;
0 ignored issues
show
Bug introduced by
The type WebStream\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use WebStream\Exception\Extend\AnnotationException;
0 ignored issues
show
Bug introduced by
The type WebStream\Exception\Extend\AnnotationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
 * Template
14
 * @author Ryuichi TANAKA.
15
 * @since 2013/10/10
16
 * @version 0.7
17
 *
18
 * @Annotation
19
 * @Target("METHOD")
20
 */
21
class Template extends Annotation implements IMethod, IRead
22
{
23
    /**
24
     * @var array<string> 注入アノテーション情報
25
     */
26
    private array $injectAnnotation;
27
28
    /**
29
     * @var array<string> 読み込みアノテーション情報
30
     */
31
    private array $readAnnotation;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 9
    public function onInject(array $injectAnnotation)
37
    {
38 9
        $this->injectAnnotation = $injectAnnotation;
39 9
        $this->readAnnotation = [];
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 5
    public function getAnnotationInfo(): array
46
    {
47 5
        return $this->readAnnotation;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 9
    public function onMethodInject(IAnnotatable $instance, \ReflectionMethod $method, Container $container)
54
    {
55 9
        $filename = array_key_exists('value', $this->injectAnnotation) ? $this->injectAnnotation['value'] : null;
56 9
        $engine = array_key_exists('engine', $this->injectAnnotation) ? $this->injectAnnotation['engine'] : "basic";
57 9
        $debug = array_key_exists('debug', $this->injectAnnotation) ? $this->injectAnnotation['debug'] : false;
58 9
        $cacheTime = array_key_exists('cacheTime', $this->injectAnnotation) ? $this->injectAnnotation['cacheTime'] : null;
59 9
        $logger = $container->logger;
60
61 9
        if (PHP_OS === "WIN32" || PHP_OS === "WINNT") {
62
            if (preg_match("/^.*[. ]|.*[\p{Cntrl}\/:*?\"<>|].*|(?i:CON|PRN|AUX|CLOCK\$|NUL|COM[1-9]|LPT[1-9])(?:[.].+)?$/", $filename)) {
63
                throw new AnnotationException("Invalid string contains in @Template('$filename')");
64
            }
65
        } else {
66 9
            if (preg_match("/:|\.\.\/|\.\.\\\\/", $filename)) {
67
                throw new AnnotationException("Invalid string contains in @Template('$filename')");
68
            }
69
        }
70
71 9
        if ($filename === null) {
72 1
            $errorMsg = "Invalid argument of @Template('$filename'). There is no specification of the base template.";
73 1
            throw new AnnotationException($errorMsg);
74
        }
75
76 8
        if ($engine === "twig") {
77 3
            if (!is_bool($debug)) {
78 1
                if ($debug !== null) {
79 1
                    $errorMsg = "Invalid argument of @Template('$filename'). 'debug' attribute bool only be specified.";
80 1
                    throw new AnnotationException($errorMsg);
81
                }
82
                $debug = false;
83
            }
84 2
            $this->readAnnotation['filename'] = $filename;
85 2
            $this->readAnnotation['engine'] = $container->engine['twig'];
86 2
            $this->readAnnotation['debug'] = $debug;
87
88 2
            if ($cacheTime !== null) {
89
                $logger->warn("'cacheTime' attribute is not used in Twig template.");
90
            }
91 5
        } elseif ($engine === "basic") {
92 4
            if ($cacheTime !== null) {
93
                // 複数指定は不可
94 2
                if (is_array($cacheTime)) {
95
                    $errorMsg = "Invalid argument of @Template attribute 'cacheTime' should not be array.";
96
                    throw new AnnotationException($errorMsg);
97
                }
98
                // 数値以外は不可
99 2
                if (!preg_match("/^[1-9][0-9]*$/", $cacheTime)) {
100 1
                    $errorMsg = "Invalid argument of @Template attribute 'cacheTime' should not be integer.";
101 1
                    throw new AnnotationException($errorMsg);
102
                }
103
104 1
                $cacheTime = intval($cacheTime);
105 1
                if ($cacheTime <= 0) {
106
                    $errorMsg = "Expire value is out of integer range: @Template(cacheTime=" . strval($cacheTime) . ")";
107
                    throw new AnnotationException($errorMsg);
108 1
                } elseif ($cacheTime >= PHP_INT_MAX) {
109
                    $logger->warn("Expire value converted the maximum of PHP Integer.");
110
                }
111
112 1
                $this->readAnnotation['cacheTime'] = $cacheTime;
113
            }
114
115 3
            $this->readAnnotation['filename'] = $filename;
116 3
            $this->readAnnotation['engine'] = $container->engine['basic'];
117
118 3
            if ($debug !== null) {
119 3
                $logger->warn("'debug' attribute is not used in Basic template.");
120
            }
121
        } else {
122 1
            $errorMsg = "Invalid 'engine' attribute of @Template('$filename').";
123 1
            throw new AnnotationException($errorMsg);
124
        }
125
    }
126
}
127