Completed
Pull Request — master (#116)
by Artem
01:15
created

EnumConstantExtension::findOccurrences()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
/*
3
 * This file is part of the FreshDoctrineEnumBundle
4
 *
5
 * (c) Artem Genvald <[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
20
/**
21
 * EnumConstantExtension.
22
 *
23
 * @author Artem Genvald <[email protected]>
24
 */
25
class EnumConstantExtension extends AbstractEnumExtension
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getFilters(): array
31
    {
32
        return [new \Twig_Filter('enum_constant', [$this, 'getEnumConstant'])];
33
    }
34
35
    /**
36
     * @param string|null $enumConstant
37
     * @param string|null $enumType
38
     *
39
     * @throws EnumTypeIsNotRegisteredException
40
     * @throws NoRegisteredEnumTypesException
41
     * @throws ConstantIsFoundInFewRegisteredEnumTypesException
42
     * @throws ConstantIsNotFoundInAnyRegisteredEnumTypeException
43
     * @throws \InvalidArgumentException
44
     * @throws \ReflectionException
45
     *
46
     * @return string
47
     */
48
    public function getEnumConstant(?string $enumConstant, ?string $enumType = null): string
49
    {
50
        if ($this->hasRegisteredEnumTypes()) {
51
            // 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...
52
            if (null !== $enumType) {
53
                $this->throwExceptionIfEnumTypeIsNotRegistered($enumType);
54
55
                return \constant($this->registeredEnumTypes[$enumType].'::'.$enumConstant);
56
            }
57
58
            // If ENUM type wasn't set, e.g. {{ 'CENTER'|enum_constant }}
59
            $this->findOccurrences($enumConstant);
60
61
            if ($this->onlyOneOccurrenceFound()) {
62
                return \constant(\array_pop($this->occurrences).'::'.$enumConstant);
63
            }
64
65
            if ($this->moreThanOneOccurrenceFound()) {
66
                throw new ConstantIsFoundInFewRegisteredEnumTypesException(
67
                    \sprintf(
68
                        'Constant "%s" is found in few registered ENUM types. You should manually set the appropriate one.',
69
                        $enumConstant
70
                    )
71
                );
72
            }
73
74
            throw new ConstantIsNotFoundInAnyRegisteredEnumTypeException(
75
                \sprintf(
76
                    'Constant "%s" was not found in any registered ENUM type.',
77
                    $enumConstant
78
                )
79
            );
80
        }
81
82
        throw $this->createNoRegisteredEnumTypesException();
83
    }
84
85
    /**
86
     * @param string $enumConstant
87
     *
88
     * @throws \ReflectionException
89
     */
90
    private function findOccurrences(string $enumConstant): void
91
    {
92
        foreach ($this->registeredEnumTypes as $registeredEnumType) {
93
            $reflection = new \ReflectionClass($registeredEnumType);
94
95
            if ($reflection->hasConstant($enumConstant)) {
96
                $this->occurrences[] = $registeredEnumType;
97
            }
98
        }
99
    }
100
}
101