These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\GeneratorBundle\Helper; |
||
4 | |||
5 | use Doctrine\ORM\Mapping\ClassMetadata; |
||
6 | use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper; |
||
7 | use Symfony\Component\Console\Input\InputInterface; |
||
8 | use Symfony\Component\Console\Output\OutputInterface; |
||
9 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
10 | use Symfony\Component\HttpKernel\Kernel; |
||
11 | |||
12 | /** |
||
13 | * GeneratorUtils |
||
14 | */ |
||
15 | class GeneratorUtils |
||
16 | { |
||
17 | /** |
||
18 | * Cleans the prefix. Prevents a double underscore from happening. |
||
19 | * |
||
20 | * @param $prefixString |
||
21 | * |
||
22 | * @return string |
||
0 ignored issues
–
show
|
|||
23 | */ |
||
24 | 9 | public static function cleanPrefix($prefixString) |
|
25 | { |
||
26 | 9 | $prefixString = trim($prefixString); |
|
27 | 9 | if (empty($prefixString)) { |
|
28 | 3 | return null; |
|
29 | } |
||
30 | |||
31 | 6 | $result = preg_replace('/_*$/i', '', strtolower($prefixString)) . '_'; |
|
32 | |||
33 | 6 | if ($result == '_') { |
|
34 | 1 | return null; |
|
35 | } |
||
36 | |||
37 | 5 | return $result; |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * Returns an array of fields. Fields can be both column fields and |
||
42 | * association fields. |
||
43 | * |
||
44 | * @return array $fields |
||
45 | */ |
||
46 | public static function getFieldsFromMetadata(ClassMetadata $metadata) |
||
47 | { |
||
48 | $fields = (array) $metadata->fieldNames; |
||
49 | |||
50 | // Remove the primary key field if it's not managed manually |
||
51 | if (!$metadata->isIdentifierNatural()) { |
||
52 | $fields = array_diff($fields, $metadata->identifier); |
||
53 | } |
||
54 | |||
55 | foreach ($metadata->associationMappings as $fieldName => $relation) { |
||
56 | if ($relation['type'] !== ClassMetadata::ONE_TO_MANY) { |
||
57 | $fields[] = $fieldName; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | return $fields; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Prepend the string in the file |
||
66 | * |
||
67 | * @param string $string Text to be added in front of the file |
||
68 | * @param string $filename File to prepend in |
||
69 | */ |
||
70 | View Code Duplication | public static function prepend($string, $filename) |
|
71 | { |
||
72 | $context = stream_context_create(); |
||
73 | $fp = fopen($filename, 'r', 1, $context); |
||
74 | $tmpname = md5($string); |
||
75 | file_put_contents($tmpname, $string); |
||
76 | file_put_contents($tmpname, $fp, FILE_APPEND); |
||
77 | fclose($fp); |
||
78 | unlink($filename); |
||
79 | rename($tmpname, $filename); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Append the string in the file |
||
84 | * |
||
85 | * @param string $string Text to be added in front of the file |
||
86 | * @param string $filename File to prepend in |
||
87 | */ |
||
88 | View Code Duplication | public static function append($string, $filename) |
|
89 | { |
||
90 | $context = stream_context_create(); |
||
91 | $fp = fopen($filename, 'r', 1, $context); |
||
92 | $tmpname = md5($string); |
||
93 | file_put_contents($tmpname, $fp); |
||
94 | file_put_contents($tmpname, $string, FILE_APPEND); |
||
95 | fclose($fp); |
||
96 | unlink($filename); |
||
97 | rename($tmpname, $filename); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Find and replace the string in the file |
||
102 | * |
||
103 | * @param string $toReplace Text to be replaced |
||
104 | * @param string $replaceText Text as replacement |
||
105 | * @param string $filename File to replace in |
||
106 | */ |
||
107 | public static function replace($toReplace, $replaceText, $filename) |
||
108 | { |
||
109 | $content = file_get_contents($filename); |
||
110 | if ($content) { |
||
111 | $content = str_replace($toReplace, $replaceText, $content); |
||
112 | file_put_contents($filename, $content); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | 1 | public static function getFullSkeletonPath($pathInSkeleton) |
|
117 | { |
||
118 | 1 | $pathInSkeleton = trim($pathInSkeleton); |
|
119 | |||
120 | // pathInSkeleton needs to be prepended by a / |
||
121 | 1 | if (substr($pathInSkeleton, 0, 1) !== '/') { |
|
122 | $pathInSkeleton = '/' . $pathInSkeleton; |
||
123 | } |
||
124 | |||
125 | // Can't have a / at the end. |
||
126 | 1 | if (substr($pathInSkeleton, -1) == '/') { |
|
127 | $pathInSkeleton = rtrim($pathInSkeleton, '/'); |
||
128 | } |
||
129 | |||
130 | 1 | return __DIR__ . '/../Resources/SensioGeneratorBundle/skeleton' . $pathInSkeleton; |
|
131 | } |
||
132 | |||
133 | public static function ensureOptionsProvided(InputInterface $input, array $options) |
||
134 | { |
||
135 | foreach ($options as $option) { |
||
136 | if (null === $input->getOption($option)) { |
||
137 | throw new \RuntimeException(sprintf('The "%s" option must be provided.', $option)); |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Replaces '\' with '/'. |
||
144 | * |
||
145 | * @param $namespace |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | public static function fixNamespace($namespace) |
||
150 | { |
||
151 | return str_replace('\\', '/', $namespace); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Returns an inputAssistant. |
||
156 | * |
||
157 | * This probably isn't the cleanest way. It'd be nicer if we could make a KunstmaanGenerator class |
||
158 | * which all generators inherit from. It then provides a bunch of helper functions and a uniform manner |
||
159 | * in which the input options are handled. |
||
160 | * |
||
161 | * @return InputAssistant |
||
162 | */ |
||
163 | public static function getInputAssistant(InputInterface &$input, OutputInterface $output, QuestionHelper $questionHelper, Kernel $kernel, ContainerInterface $container) |
||
164 | { |
||
165 | return new InputAssistant($input, $output, $questionHelper, $kernel, $container); |
||
166 | } |
||
167 | } |
||
168 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.