• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tochka-developers / jsonrpc / 4135501466

pending completion
4135501466

push

github

darkdarin
Merge remote-tracking branch 'origin/v5.0'

209 of 813 new or added lines in 51 files covered. (25.71%)

233 of 1307 relevant lines covered (17.83%)

1.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Support/DocBlockFactory.php
1
<?php
2

3
namespace Tochka\JsonRpc\Support;
4

5
use phpDocumentor\Reflection\DocBlockFactory as ReflectionDocBlock;
6
use Tochka\JsonRpc\Contracts\DocBlockFactoryInterface;
7
use Tochka\JsonRpc\Contracts\AnnotationReaderInterface;
8

9
/**
10
 * @psalm-api
11
 */
12
class DocBlockFactory implements DocBlockFactoryInterface
13
{
14
    /** @var array<string, JsonRpcDocBlock> */
15
    private array $docBlocks = [];
16
    private AnnotationReaderInterface $annotationReader;
17
    private ReflectionDocBlock $docBlockFactory;
18

19
    /**
20
     * @psalm-suppress PossiblyUnusedMethod
21
     */
22
    public function __construct(ReflectionDocBlock $docBlockFactory, AnnotationReaderInterface $annotationReader)
23
    {
24
        $this->docBlockFactory = $docBlockFactory;
×
NEW
25
        $this->annotationReader = $annotationReader;
×
26
    }
27

28
    public function make(\Reflector $reflector): ?JsonRpcDocBlock
29
    {
NEW
30
        $callable = static function () use ($reflector): \Reflector {
×
31
            return $reflector;
×
32
        };
×
33

34
        if ($reflector instanceof \ReflectionClass) {
×
35
            return $this->getOrMakeFromReflection(
×
36
                $this->getKeyForClass($reflector->getName()),
×
37
                $callable
×
38
            );
×
39
        }
40

41
        if ($reflector instanceof \ReflectionMethod) {
×
42
            return $this->getOrMakeFromReflection(
×
43
                $this->getKeyForMethod($reflector->getDeclaringClass()->getName(), $reflector->getName()),
×
44
                $callable
×
45
            );
×
46
        }
47

48
        if ($reflector instanceof \ReflectionProperty) {
×
49
            return $this->getOrMakeFromReflection(
×
50
                $this->getKeyForProperty($reflector->getDeclaringClass()->getName(), $reflector->getName()),
×
51
                $callable
×
52
            );
×
53
        }
54

55
        if ($reflector instanceof \ReflectionFunction) {
×
56
            return $this->getOrMakeFromReflection(
×
57
                $this->getKeyForFunction($reflector->getName()),
×
58
                $callable
×
59
            );
×
60
        }
61

62
        if ($reflector instanceof \ReflectionParameter) {
×
63
            return $this->getOrMakeFromReflection(
×
64
                $this->getKeyForParameter(
×
NEW
65
                    $reflector->getDeclaringFunction()->getName(),
×
66
                    $reflector->getName(),
×
NEW
67
                    $reflector->getDeclaringClass()?->getName()
×
68
                ),
×
69
                $callable
×
70
            );
×
71
        }
72
        if ($reflector instanceof \ReflectionClassConstant) {
×
73
            return $this->getOrMakeFromReflection(
×
74
                $this->getKeyForClassConstant($reflector->getDeclaringClass()->getName(), $reflector->getName()),
×
75
                $callable
×
76
            );
×
77
        }
78

79
        return null;
×
80
    }
81

82
    public function makeForClass(string $className): ?JsonRpcDocBlock
83
    {
84
        try {
85
            return $this->getOrMakeFromReflection(
×
86
                $this->getKeyForClass($className),
×
87
                function () use ($className) {
×
88
                    return new \ReflectionClass($className);
×
89
                }
×
90
            );
×
NEW
91
        } catch (\ReflectionException) {
×
92
            return null;
×
93
        }
94
    }
95

96
    public function makeForMethod(string $className, string $methodName): ?JsonRpcDocBlock
97
    {
98
        try {
99
            return $this->getOrMakeFromReflection(
×
100
                $this->getKeyForMethod($className, $methodName),
×
101
                function () use ($className, $methodName) {
×
102
                    return new \ReflectionMethod($className, $methodName);
×
103
                }
×
104
            );
×
NEW
105
        } catch (\ReflectionException) {
×
106
            return null;
×
107
        }
108
    }
109

110
    public function makeForProperty(string $className, string $propertyName): ?JsonRpcDocBlock
111
    {
112
        try {
113
            return $this->getOrMakeFromReflection(
×
114
                $this->getKeyForProperty($className, $propertyName),
×
115
                function () use ($className, $propertyName) {
×
116
                    return new \ReflectionProperty($className, $propertyName);
×
117
                }
×
118
            );
×
NEW
119
        } catch (\ReflectionException) {
×
120
            return null;
×
121
        }
122
    }
123

124
     public function makeForParameter(string $className, string $methodName, string $parameterName): ?JsonRpcDocBlock
125
     {
126
         try {
NEW
127
             return $this->getOrMakeFromReflection(
×
NEW
128
                 $this->getKeyForParameter($className, $methodName, $parameterName),
×
NEW
129
                 function () use ($className, $methodName, $parameterName) {
×
NEW
130
                     return new \ReflectionParameter([$className, $methodName], $parameterName);
×
NEW
131
                 }
×
NEW
132
             );
×
NEW
133
         } catch (\ReflectionException) {
×
NEW
134
             return null;
×
135
         }
136
     }
137

138
    /**
139
      * @param string $key
140
      * @param callable(): \Reflector $reflectorCallable
141
      * @return JsonRpcDocBlock
142
      */
143
    private function getOrMakeFromReflection(string $key, callable $reflectorCallable): JsonRpcDocBlock
144
    {
145
        if (!array_key_exists($key, $this->docBlocks)) {
×
146
            $this->docBlocks[$key] = new JsonRpcDocBlock(
×
147
                $reflectorCallable(),
×
148
                $this->annotationReader,
×
149
                $this->docBlockFactory
×
150
            );
×
151
        }
152

153
        return $this->docBlocks[$key];
×
154
    }
155

156
    private function getKeyForClass(string $className): string
157
    {
158
        return sprintf('class:%s', $className);
×
159
    }
160

161
    private function getKeyForMethod(string $className, string $methodName): string
162
    {
163
        return sprintf('method:%s:%s', $className, $methodName);
×
164
    }
165

166
    private function getKeyForProperty(string $className, string $propertyName): string
167
    {
168
        return sprintf('property:%s:%s', $className, $propertyName);
×
169
    }
170

171
    private function getKeyForFunction(string $functionName): string
172
    {
173
        return sprintf('function:%s', $functionName);
×
174
    }
175

176
    private function getKeyForParameter(string $methodName, string $parameterName, ?string $className = null): string
177
    {
178
        if ($className === null) {
×
179
            $className = '$global$';
×
180
        }
181
        return sprintf('parameter:%s:%s:%s', $className, $methodName, $parameterName);
×
182
    }
183

184
    private function getKeyForClassConstant(string $className, string $constantName): string
185
    {
186
        return sprintf('classConstant:%s:%s', $className, $constantName);
×
187
    }
188
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc