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

RonasIT / laravel-swagger / 26497152981

27 May 2026 07:25AM UTC coverage: 99.569% (-0.09%) from 99.656%
26497152981

Pull #193

github

web-flow
Merge 4b399b27a into a9abd440c
Pull Request #193: feat: name response object by resource class

121 of 122 new or added lines in 8 files covered. (99.18%)

1 existing line in 1 file now uncovered.

924 of 928 relevant lines covered (99.57%)

23.46 hits per line

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

96.55
/src/RequestContext/Extractors/BaseControllerExtractor.php
1
<?php
2

3
namespace RonasIT\AutoDoc\RequestContext\Extractors;
4

5
use Illuminate\Http\Resources\Json\JsonResource;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
use ReflectionFunctionAbstract;
9

10
abstract class BaseControllerExtractor
11
{
12
    public readonly ?string $resource;
13

14
    private array $fileContent;
15

16
    public function __construct()
17
    {
18
        $class = $this->getResourceClass();
34✔
19

20
        $this->resource = (!empty($class) && $this->isResourceClass($class)) ? $this->extractClassName($class) : null;
34✔
21
    }
22

23
    abstract protected function getResourceClass(): ?string;
24

25
    protected function isResourceClass(string $className): bool
26
    {
27
        return is_subclass_of($className, JsonResource::class);
6✔
28
    }
29

30
    protected function extractClassName(string $namespace): string
31
    {
32
        return Str::afterLast($namespace, '\\');
5✔
33
    }
34

35
    protected function getResourceFromCode(ReflectionFunctionAbstract $reflectionMethod): ?string
36
    {
37
        $code = $this->getFunctionCode($reflectionMethod);
31✔
38

39
        preg_match('/(?:return\s+|=>\s+)([^\s(]+)::make/', $code, $matches);
31✔
40

41
        $resourceName = $matches[1] ?? null;
31✔
42

43
        if (empty($resourceName)) {
31✔
44
            return null;
27✔
45
        }
46

47
        return (class_exists($resourceName))
4✔
48
            ? $resourceName
1✔
49
            : $this->getClassNameFromImports($reflectionMethod, $resourceName);
4✔
50
    }
51

52
    protected function getFunctionCode(ReflectionFunctionAbstract $reflectionFunction): string
53
    {
54
        $fileContent = $this->getFileContent($reflectionFunction);
31✔
55

56
        $startLineIndex = $reflectionFunction->getStartLine() - 1;
31✔
57

58
        $methodSlice = array_slice($fileContent, $startLineIndex, $reflectionFunction->getEndLine() - $startLineIndex);
31✔
59

60
        return implode('', $methodSlice);
31✔
61
    }
62

63
    protected function getClassNameFromImports(ReflectionFunctionAbstract $reflectionMethod, string $resourceName): string
64
    {
65
        $resourceImport = Arr::first(
3✔
66
            array: $this->getFileContent($reflectionMethod),
3✔
67
            callback: fn (string $line) => (Str::startsWith($line, 'use') && Str::contains($line, $resourceName)),
3✔
68
            default: '',
3✔
69
        );
3✔
70

71
        preg_match('/^use\s+([^;]+?)(?:\s+as\s+\w+)?;$/', trim($resourceImport), $matches);
3✔
72

73
        return $matches[1] ?? '';
3✔
74
    }
75

76
    protected function getFileContent(ReflectionFunctionAbstract $reflectionFunction): array
77
    {
78
        if (!isset($this->fileContent)) {
31✔
79
            $fileName = $reflectionFunction->getFileName();
31✔
80

81
            $this->fileContent = (empty($fileName) || !is_readable($fileName))
31✔
NEW
82
                ? []
×
83
                : file($fileName) ?? [];
31✔
84
        }
85

86
        return $this->fileContent;
31✔
87
    }
88
}
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

© 2026 Coveralls, Inc