Completed
Push — master ( f7badd...24295a )
by Maxime
14:47
created

EnumExtension   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 68
Duplicated Lines 70.59 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 3
dl 48
loc 68
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getFunctions() 0 11 1
A get() 8 8 2
A values() 8 8 2
A accepts() 8 8 2
A instances() 8 8 2
A readables() 8 8 2
A readableFor() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the "elao/enum" package.
5
 *
6
 * Copyright (C) Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\Enum\Bridge\Twig\Extension;
12
13
use Elao\Enum\EnumInterface;
14
use Elao\Enum\Exception\InvalidArgumentException;
15
use Elao\Enum\ReadableEnumInterface;
16
use Twig\Extension\AbstractExtension;
17
use Twig\TwigFunction;
18
19
class EnumExtension extends AbstractExtension
20
{
21
    public function getFunctions()
22
    {
23
        return [
24
            new TwigFunction('enum_get', [$this, 'get']),
25
            new TwigFunction('enum_values', [$this, 'values']),
26
            new TwigFunction('enum_accepts', [$this, 'accepts']),
27
            new TwigFunction('enum_instances', [$this, 'instances']),
28
            new TwigFunction('enum_readables', [$this, 'readables']),
29
            new TwigFunction('enum_readable_for', [$this, 'readableFor']),
30
        ];
31
    }
32
33 View Code Duplication
    public function get(string $className, $value): EnumInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        if (!is_a($className, EnumInterface::class, true)) {
36
            throw new InvalidArgumentException(sprintf('"%s" is not an "%s".', $className, EnumInterface::class));
37
        }
38
39
        return \call_user_func([$className, 'get'], $value);
40
    }
41
42 View Code Duplication
    public function values(string $className): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        if (!is_a($className, EnumInterface::class, true)) {
45
            throw new InvalidArgumentException(sprintf('"%s" is not an "%s".', $className, EnumInterface::class));
46
        }
47
48
        return \call_user_func([$className, 'values']);
49
    }
50
51 View Code Duplication
    public function accepts(string $className, $value): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        if (!is_a($className, EnumInterface::class, true)) {
54
            throw new InvalidArgumentException(sprintf('"%s" is not an "%s".', $className, EnumInterface::class));
55
        }
56
57
        return \call_user_func([$className, 'accepts'], $value);
58
    }
59
60 View Code Duplication
    public function instances(string $className): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62
        if (!is_a($className, EnumInterface::class, true)) {
63
            throw new InvalidArgumentException(sprintf('"%s" is not an "%s".', $className, EnumInterface::class));
64
        }
65
66
        return \call_user_func([$className, 'instances']);
67
    }
68
69 View Code Duplication
    public function readables(string $className): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        if (!is_a($className, ReadableEnumInterface::class, true)) {
72
            throw new InvalidArgumentException(sprintf('"%s" is not a "%s".', $className, ReadableEnumInterface::class));
73
        }
74
75
        return \call_user_func([$className, 'readables']);
76
    }
77
78 View Code Duplication
    public function readableFor(string $className, $value): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        if (!is_a($className, ReadableEnumInterface::class, true)) {
81
            throw new InvalidArgumentException(sprintf('"%s" is not a "%s".', $className, ReadableEnumInterface::class));
82
        }
83
84
        return \call_user_func([$className, 'readableFor'], $value);
85
    }
86
}
87