Header   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
eloc 59
c 0
b 0
f 0
dl 0
loc 110
ccs 33
cts 33
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B onMethodInject() 0 46 10
A onInject() 0 5 1
A getAnnotationInfo() 0 3 1
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
use WebStream\Exception\Extend\InvalidRequestException;
0 ignored issues
show
Bug introduced by
The type WebStream\Exception\Extend\InvalidRequestException 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...
12
13
/**
14
 * Header
15
 * @author Ryuichi TANAKA.
16
 * @since 2013/10/20
17
 * @version 0.4
18
 *
19
 * @Annotation
20
 * @Target("METHOD")
21
 */
22
class Header extends Annotation implements IMethod, IRead
23
{
24
    /**
25
     * @var array<string> 注入アノテーション情報
26
     */
27
    private array $injectAnnotation;
28
29
    /**
30
     * @var array<string> 読み込みアノテーション情報
31
     */
32
    private array $readAnnotation;
33
34
    /**
35
     * @var array<string, string> mimeタイプリスト
36
     */
37
    private array $contentTypeList = [
38
        'txt' => 'text/plain',
39
        'jpeg' => 'image/jpeg',
40
        'jpg' => 'image/jpeg',
41
        'gif' => 'image/gif',
42
        'png' => 'image/png',
43
        'tiff' => 'image/tiff',
44
        'tif' => 'image/tiff',
45
        'bmp' => 'image/bmp',
46
        'ico' => 'image/x-icon',
47
        'svg' => 'image/svg+xml',
48
        'xml' => 'application/xml',
49
        'xsl' => 'application/xml',
50
        'rss' => 'application/rss+xml',
51
        'rdf' => 'application/rdf+xml',
52
        'atom' => 'application/atom+xml',
53
        'zip' => 'application/zip',
54
        'html' => 'text/html',
55
        'htm' => 'text/html',
56
        'css' => 'text/css',
57
        'csv' => 'text/csv',
58
        'js' => 'text/javascript',
59
        'jsonp' => 'text/javascript',
60
        'json' => 'application/json',
61
        'pdf' => 'application/pdf',
62
        'file' => 'application/octet-stream'
63
    ];
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 16
    public function onInject(array $injectAnnotation)
69
    {
70 16
        $defaultInjectAnnotation = ['allowMethod' => null, 'contentType' => null];
71 16
        $this->injectAnnotation = array_merge($defaultInjectAnnotation, $injectAnnotation);
72 16
        $this->readAnnotation = [];
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 5
    public function getAnnotationInfo(): array
79
    {
80 5
        return $this->readAnnotation;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 11
    public function onMethodInject(IAnnotatable $instance, \ReflectionMethod $method, Container $container)
87
    {
88 11
        $allowMethods = $this->injectAnnotation['allowMethod'];
89 11
        $logger = $container->logger;
90
91
        // 指定無しの場合はチェックしない(すべてのメソッドを許可する)
92 11
        if ($allowMethods !== null) {
93 8
            if (!is_array($allowMethods)) {
94 6
                $allowMethods = [$allowMethods];
95
            }
96
97 8
            for ($i = 0; $i < count($allowMethods); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
98 8
                if (!preg_match("/^(?:(?:P(?:OS|U)|GE)T|(?:p(?:os|u)|ge)t|DELETE|delete)$/", $allowMethods[$i])) {
99 1
                    $errorMsg = "Invalid value '" . $allowMethods[$i] . "' in 'allowMethod' attribute of @Header.";
100 1
                    throw new AnnotationException($errorMsg);
101
                }
102 7
                $allowMethods[$i] = strtoupper($allowMethods[$i]);
103
            }
104
105
            // 複数指定した場合、一つでも許可されていればOK
106 7
            if (!array_key_exists($container->requestMethod, array_flip($allowMethods))) {
107 3
                $errorMsg = "Not allowed request method '" . $container->requestMethod;
108 3
                throw new InvalidRequestException($errorMsg);
109
            }
110
111 4
            $logger->debug("Accepted method '" . implode(',', $allowMethods) . "'");
112
        }
113
114 7
        $ext = $this->injectAnnotation['contentType'] ?: 'html';
115
116 7
        if (!is_string($ext)) {
117 1
            $errorMsg = "contentType' attribute of @Header must be string.";
118 1
            throw new AnnotationException($errorMsg);
119
        }
120
121 6
        $contentType = null;
122 6
        if (array_key_exists($ext, $this->contentTypeList)) {
123 5
            $contentType = $this->contentTypeList[$ext];
124
        }
125 6
        if ($contentType === null) {
126 1
            $errorMsg = "Invalid value '$ext' in 'contentType' attribute of @Header.";
127 1
            throw new AnnotationException($errorMsg);
128
        }
129
130 5
        $this->readAnnotation['contentType'] = $ext;
131 5
        $logger->debug("Accepted contentType '$ext'");
132
    }
133
}
134