Completed
Push — master ( 29a4f7...8a3bcf )
by Artem
04:10
created

EnumConstantExtension::getFilters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
namespace Fresh\DoctrineEnumBundle\Twig\Extension;
12
13
use Fresh\DoctrineEnumBundle\Exception\EnumTypeIsNotRegisteredException;
14
use Fresh\DoctrineEnumBundle\Exception\NoRegisteredEnumTypesException;
15
use Fresh\DoctrineEnumBundle\Exception\ConstantIsFoundInFewRegisteredEnumTypesException;
16
use Fresh\DoctrineEnumBundle\Exception\ConstantIsNotFoundInAnyRegisteredEnumTypeException;
17
18
/**
19
 * EnumConstantExtension
20
 *
21
 * @author Artem Genvald <[email protected]>
22
 */
23
class EnumConstantExtension extends AbstractEnumExtension
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getFilters()
29
    {
30
        return [new \Twig_SimpleFilter('enum_constant', [$this, 'getEnumConstant'])];
31
    }
32
33
    /**
34
     * Get value of the ENUM constant
35
     *
36
     * @param string      $enumConstant ENUM constant
37
     * @param string|null $enumType     ENUM type
38
     *
39
     * @throws EnumTypeIsNotRegisteredException
40
     * @throws NoRegisteredEnumTypesException
41
     * @throws ConstantIsFoundInFewRegisteredEnumTypesException
42
     * @throws ConstantIsNotFoundInAnyRegisteredEnumTypeException
43
     *
44
     * @return string
45
     */
46
    public function getEnumConstant($enumConstant, $enumType = null)
47
    {
48
        if (!empty($this->registeredEnumTypes) && is_array($this->registeredEnumTypes)) {
49
            // 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...
50
            if (!empty($enumType)) {
51
                if (!isset($this->registeredEnumTypes[$enumType])) {
52
                    throw new EnumTypeIsNotRegisteredException(sprintf('ENUM type "%s" is not registered.', $enumType));
53
                }
54
55
                return constant($this->registeredEnumTypes[$enumType].'::'.$enumConstant);
56
            } else {
57
                // If ENUM type wasn't set, e.g. {{ 'CENTER'|enum_constant }}
58
                $occurrences = [];
59
                // Check if constant exists in registered ENUM types
60
                foreach ($this->registeredEnumTypes as $registeredEnumType) {
61
                    $reflection = new \ReflectionClass($registeredEnumType);
62
                    if ($reflection->hasConstant($enumConstant)) {
63
                        $occurrences[] = $registeredEnumType;
64
                    }
65
                }
66
67
                // If found only one occurrence, then we know exactly which ENUM type
68
                if (count($occurrences) == 1) {
69
                    $enumClassName = array_pop($occurrences);
70
71
                    return constant($enumClassName.'::'.$enumConstant);
72
                } elseif (count($occurrences) > 1) {
73
                    throw new ConstantIsFoundInFewRegisteredEnumTypesException(sprintf(
74
                        'Constant "%s" is found in few registered ENUM types. You should manually set the appropriate one.',
75
                        $enumConstant
76
                    ));
77
                } else {
78
                    throw new ConstantIsNotFoundInAnyRegisteredEnumTypeException(sprintf(
79
                        'Constant "%s" wasn\'t found in any registered ENUM type.',
80
                        $enumConstant
81
                    ));
82
                }
83
            }
84
        } else {
85
            throw new NoRegisteredEnumTypesException('There are no registered ENUM types.');
86
        }
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getName()
93
    {
94
        return 'ENUM Constant';
95
    }
96
}
97