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

DerManoMann / openapi-extras / 11585037225

30 Oct 2024 01:01AM UTC coverage: 97.196% (-1.6%) from 98.795%
11585037225

push

github

web-flow
Add processor to auto generate a basic description for enum backed properties (#11)

24 of 26 new or added lines in 2 files covered. (92.31%)

104 of 107 relevant lines covered (97.2%)

2.04 hits per line

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

88.24
/src/Processors/EnumDescription.php
1
<?php declare(strict_types=1);
2

3
namespace Radebatz\OpenApi\Extras\Processors;
4

5
use OpenApi\Analysis;
6
use OpenApi\Annotations as OA;
7
use OpenApi\Generator;
8

9
/**
10
 * Generates a description for enum based properties.
11
 */
12
class EnumDescription
13
{
14
    protected $enabled = false;
15

16
    public function __construct(bool $enabled = false)
17
    {
18
        $this->enabled = $enabled;
2✔
19
    }
20

21
    public function isEnabled(): bool
22
    {
NEW
23
        return $this->enabled;
×
24
    }
25

26
    /**
27
     * Enables/disables the <code>EnumDescription</code> processor.
28
     */
29
    public function setEnabled(bool $enabled): EnumDescription
30
    {
31
        $this->enabled = $enabled;
2✔
32

33
        return $this;
2✔
34
    }
35

36
    public function __invoke(Analysis $analysis)
37
    {
38
        if (!class_exists('\\ReflectionEnum')) {
2✔
NEW
39
            return;
×
40
        }
41

42
        /** @var OA\Property[] $properties */
43
        $properties = $analysis->getAnnotationsOfType(OA\Property::class);
2✔
44

45
        foreach ($properties as $property) {
2✔
46
            if (!Generator::isDefault($property->enum) && enum_exists($property->enum) && Generator::isDefault($property->description)) {
2✔
47
                $re = new \ReflectionEnum($property->enum);
2✔
48
                $values = [];
2✔
49
                if ($re->isBacked()) {
2✔
50
                    foreach ($re->getCases() as $case) {
2✔
51
                        $values[] = $case->getBackingValue() . ':' . $case->getName();
2✔
52
                    }
53
                } else {
54
                    foreach ($re->getCases() as $case) {
2✔
55
                        $values[] = $case->getName();
2✔
56
                    }
57
                }
58

59
                $property->description = $re->getShortName() . ' (' . implode('; ', $values) . ')';
2✔
60
            }
61
        }
62
    }
63
}
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