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

cnizzardini / cakephp-swagger-bake / 24732360658

21 Apr 2026 03:53PM UTC coverage: 95.292% (-0.07%) from 95.357%
24732360658

Pull #582

github

web-flow
Merge 64272f377 into e52c954b3
Pull Request #582: Fix OperationResponseAssociation ignoring controller defaultTable

4 of 6 new or added lines in 1 file covered. (66.67%)

12 existing lines in 2 files now uncovered.

2591 of 2719 relevant lines covered (95.29%)

37.26 hits per line

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

86.11
/src/Lib/Operation/ExceptionResponse.php
1
<?php
2
declare(strict_types=1);
3

4
namespace SwaggerBake\Lib\Operation;
5

6
use Cake\Core\Exception\CakeException;
7
use Exception;
8
use phpDocumentor\Reflection\DocBlock\Tags\Throws;
9
use ReflectionClass;
10
use SwaggerBake\Lib\Configuration;
11
use SwaggerBake\Lib\OpenApi\Schema;
12
use SwaggerBake\Lib\OpenApiExceptionSchemaInterface;
13
use Throwable;
14

15
/**
16
 * Defines an error responses.
17
 */
18
class ExceptionResponse
19
{
20
    private ?string $description = null;
21

22
    private string $code = '';
23

24
    private Schema|string|null $schema = null;
25

26
    /**
27
     * @param \SwaggerBake\Lib\Configuration $config Configuration
28
     */
29
    public function __construct(private Configuration $config)
30
    {
31
    }
71✔
32

33
    /**
34
     * Attempts finding exception Schema or OpenAPI $ref based on the FQN of `@throws` in the following order:
35
     *
36
     * 1. Checks for exceptions implementing OpenApiExceptionSchemaInterface.
37
     * 2. Checks for CakePHP exceptions.
38
     * ~~3. Searches in x-swagger-bake/components/app-exceptions for custom exception schema (deprecated).~~
39
     * 4. Uses the default exception as defined in your swagger_bake config.
40
     *
41
     * If an implementation is found, the code, description, and schema will be defined from it.
42
     *
43
     * @see \SwaggerBake\Lib\OpenApiExceptionSchemaInterface
44
     * @param \phpDocumentor\Reflection\DocBlock\Tags\Throws $throw The exception to be thrown
45
     * @return $this
46
     */
47
    public function build(Throws $throw)
48
    {
49
        $exceptionFqn = $throw->getType()->__toString();
71✔
50

51
        try {
52
            if (!class_exists($exceptionFqn)) {
71✔
53
                throw new Exception("Class $exceptionFqn does not exist");
×
54
            }
55
            $reflection = new ReflectionClass($exceptionFqn);
71✔
56
            if ($reflection->implementsInterface(OpenApiExceptionSchemaInterface::class)) {
71✔
57
                $this->code = $exceptionFqn::getExceptionCode();
1✔
58
                $this->description = $exceptionFqn::getExceptionDescription();
1✔
59
                $this->schema = $exceptionFqn::getExceptionSchema();
1✔
60

61
                return $this;
71✔
62
            }
63
        } catch (Throwable $e) {
×
64
            $reflection = null;
×
65
        }
66

67
        $httpCode = null;
70✔
68
        $description = $throw->getDescription()->getBodyTemplate();
70✔
69

70
        if ($reflection) {
70✔
71
            $instance = $reflection->newInstanceWithoutConstructor();
70✔
72
            if ($reflection->hasProperty('_defaultCode')) {
70✔
73
                $reflectedProperty = $reflection->getProperty('_defaultCode');
67✔
74
                $reflectedProperty->setAccessible(true);
67✔
75
                $httpCode = (string)$reflectedProperty->getValue($instance);
67✔
76
            } elseif ($instance instanceof CakeException && $instance->getCode() > 0) {
25✔
UNCOV
77
                $httpCode = (string)$instance->getCode();
×
78
            }
79
            if (empty($description)) {
70✔
80
                $description = $reflection->getShortName();
35✔
81
            }
82
        }
83

84
        if ($exceptionFqn == '\Cake\Datasource\Exception\RecordNotFoundException') {
70✔
85
            $httpCode = '404';
63✔
86
        }
87

88
        $this->code = empty($httpCode) ? '500' : $httpCode;
70✔
89
        $this->description = $description;
70✔
90
        $this->schema = $this->fallback();
70✔
91

92
        return $this;
70✔
93
    }
94

95
    /**
96
     * @deprecated this method may be removed in version 3.
97
     */
98
    private function fallback(): ?string
99
    {
100
        if (empty($this->config->getExceptionSchema())) {
70✔
UNCOV
101
            return null;
×
102
        }
103

104
        return '#/components/schemas/' . $this->config->getExceptionSchema();
70✔
105
    }
106

107
    /**
108
     * The HTTP status code associated with the exception.
109
     *
110
     * @link https://spec.openapis.org/oas/v3.0.3#responses-object
111
     * @return string
112
     */
113
    public function getCode(): string
114
    {
115
        return $this->code;
68✔
116
    }
117

118
    /**
119
     * The response description associated with the exception.
120
     *
121
     * @link https://spec.openapis.org/oas/v3.0.3#responses-object
122
     * @return string|null
123
     */
124
    public function getDescription(): ?string
125
    {
126
        return $this->description;
63✔
127
    }
128

129
    /**
130
     * The OpenAPI Schema or $ref string defining the exception.
131
     *
132
     * @see \SwaggerBake\Lib\OpenApi\Schema
133
     * @return \SwaggerBake\Lib\OpenApi\Schema|string|null
134
     */
135
    public function getSchema(): Schema|string|null
136
    {
137
        return $this->schema;
64✔
138
    }
139
}
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