Completed
Push — master ( 56c365...b11c50 )
by Artem
12s
created

EnumConstantTwigExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 76
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilters() 0 4 1
B getEnumConstant() 0 36 5
A findOccurrences() 0 10 3
1
<?php
2
/*
3
 * This file is part of the FreshDoctrineEnumBundle
4
 *
5
 * (c) Artem Henvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fresh\DoctrineEnumBundle\Twig\Extension;
14
15
use Fresh\DoctrineEnumBundle\Exception\Constant\ConstantIsFoundInFewRegisteredEnumTypesException;
16
use Fresh\DoctrineEnumBundle\Exception\Constant\ConstantIsNotFoundInAnyRegisteredEnumTypeException;
17
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsNotRegisteredException;
18
use Fresh\DoctrineEnumBundle\Exception\EnumType\NoRegisteredEnumTypesException;
19
use Twig\TwigFilter;
20
21
/**
22
 * EnumConstantExtension.
23
 *
24
 * @author Artem Henvald <[email protected]>
25
 */
26
class EnumConstantTwigExtension extends AbstractEnumTwigExtension
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getFilters(): array
32
    {
33
        return [new TwigFilter('enum_constant', [$this, 'getEnumConstant'])];
34
    }
35
36
    /**
37
     * @param string|null $enumConstant
38
     * @param string|null $enumType
39
     *
40
     * @throws EnumTypeIsNotRegisteredException
41
     * @throws NoRegisteredEnumTypesException
42
     * @throws ConstantIsFoundInFewRegisteredEnumTypesException
43
     * @throws ConstantIsNotFoundInAnyRegisteredEnumTypeException
44
     * @throws \InvalidArgumentException
45
     * @throws \ReflectionException
46
     *
47
     * @return string
48
     */
49
    public function getEnumConstant(?string $enumConstant, ?string $enumType = null): string
50
    {
51
        if ($this->hasRegisteredEnumTypes()) {
52
            // If ENUM type was set, e.g. {{ 'CENTER'|enum_constant('BasketballPositionType') }}
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
53
            if (null !== $enumType) {
54
                $this->throwExceptionIfEnumTypeIsNotRegistered($enumType);
55
56
                return \constant($this->registeredEnumTypes[$enumType].'::'.$enumConstant);
57
            }
58
59
            // If ENUM type wasn't set, e.g. {{ 'CENTER'|enum_constant }}
60
            $this->findOccurrences($enumConstant);
61
62
            if ($this->onlyOneOccurrenceFound()) {
63
                return \constant(\array_pop($this->occurrences).'::'.$enumConstant);
64
            }
65
66
            if ($this->moreThanOneOccurrenceFound()) {
67
                throw new ConstantIsFoundInFewRegisteredEnumTypesException(
68
                    \sprintf(
69
                        'Constant "%s" is found in few registered ENUM types. You should manually set the appropriate one.',
70
                        $enumConstant
71
                    )
72
                );
73
            }
74
75
            throw new ConstantIsNotFoundInAnyRegisteredEnumTypeException(
76
                \sprintf(
77
                    'Constant "%s" was not found in any registered ENUM type.',
78
                    $enumConstant
79
                )
80
            );
81
        }
82
83
        throw $this->createNoRegisteredEnumTypesException();
84
    }
85
86
    /**
87
     * @param string $enumConstant
88
     *
89
     * @throws \ReflectionException
90
     */
91
    private function findOccurrences(string $enumConstant): void
92
    {
93
        foreach ($this->registeredEnumTypes as $registeredEnumType) {
94
            $reflection = new \ReflectionClass($registeredEnumType);
95
96
            if ($reflection->hasConstant($enumConstant)) {
97
                $this->occurrences[] = $registeredEnumType;
98
            }
99
        }
100
    }
101
}
102