BundleStructureHelper   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 81
rs 10
ccs 37
cts 37
cp 1

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __call() 0 16 3
A getClassSuffix() 0 4 2
A getFullClass() 0 4 1
A getClass() 0 4 1
A getNamespace() 0 4 1
A getFullFilename() 0 5 2
A getFilename() 0 4 2
A getDirname() 0 4 1
A camelToUnderscore() 0 4 1
A underscoreToCamel() 0 4 1
A getBundleNamespace() 0 4 1
1
<?php
2
3
namespace Pgs\RestfonyBundle\Generator\Helper;
4
5
use Symfony\Component\DependencyInjection\Container;
6
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
7
8
class BundleStructureHelper
9
{
10
    protected $bundleRoot;
11
    protected $bundleNamespace;
12
    protected $entityClass;
13
14 24
    public function __construct(BundleInterface $bundle, $entityClass)
15
    {
16 24
        $this->bundleRoot = $bundle->getPath();
17 24
        $this->bundleNamespace = $bundle->getNamespace();
18 24
        $this->entityClass = $entityClass;
19 24
    }
20
21 20
    public function __call($name, $args)
22
    {
23 20
        $matches = [];
24 20
        preg_match('/^get([A-Z][a-z]+)((Full)?[A-Z][a-z]+)$/', $name, $matches);
25 20
        if (empty($matches)) {
26 1
            throw new \Exception('Unsupported method: ' . $name);
27
        }
28 19
        $category = $matches[1];
29 19
        $type = $matches[2];
30
31 19
        $method = 'get' . $type;
32 19
        if (!method_exists($this, $method)) {
33 1
            throw new \Exception('Unsupported method: ' . $name);
34
        }
35 18
        return $this->$method($category, $args);
36
    }
37
38 17
    protected function getClassSuffix($suffix)
39
    {
40 17
        return $suffix === 'Entity' ? '' : $suffix;
41
    }
42
43 4
    protected function getFullClass($category, $args)
44
    {
45 4
        return $this->getNamespace($category, $args) . '\\' . $this->entityClass . $this->getClassSuffix($category);
0 ignored issues
show
Unused Code introduced by
The call to BundleStructureHelper::getNamespace() has too many arguments starting with $args.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
46
    }
47
48 12
    protected function getClass($category)
49
    {
50 12
        return basename($this->entityClass . $this->getClassSuffix($category));
51
    }
52
53 5
    protected function getNamespace($category)
54
    {
55 5
        return $this->bundleNamespace . '\\' . $category;
56
    }
57
58 10
    protected function getFullFilename($category, $args)
59
    {
60 10
        $filename = isset($args[0]) ? $args[0] : (str_replace('\\', '/', $this->entityClass) . $this->getClassSuffix($category) . '.php');
61 10
        return $this->getDirname($category, $args) . '/' . $filename;
0 ignored issues
show
Unused Code introduced by
The call to BundleStructureHelper::getDirname() has too many arguments starting with $args.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
62
    }
63
64 2
    protected function getFilename($category, $args)
65
    {
66 2
        return basename(isset($args[0]) ? $args[0] : (str_replace('\\', '/', $this->entityClass) . $this->getClassSuffix($category) . '.php'));
67
    }
68
69 14
    protected function getDirname($category)
70
    {
71 14
        return $this->bundleRoot . '/' . $category;
72
    }
73
74 3
    public function camelToUnderscore($string)
75
    {
76 3
        return Container::underscore($string);
77
    }
78
79 1
    public function underscoreToCamel($string)
80
    {
81 1
        return Container::camelize($string);
82
    }
83
84 2
    public function getBundleNamespace()
85
    {
86 2
        return $this->bundleNamespace;
87
    }
88
}
89