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); |
|
|
|
|
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; |
|
|
|
|
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
|
|
|
|
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.