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 |
||
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 | * @param ClassMetadata $metadata |
||
45 | * |
||
46 | * @return array $fields |
||
47 | */ |
||
48 | public static function getFieldsFromMetadata(ClassMetadata $metadata) |
||
49 | { |
||
50 | $fields = (array) $metadata->fieldNames; |
||
51 | |||
52 | // Remove the primary key field if it's not managed manually |
||
53 | if (!$metadata->isIdentifierNatural()) { |
||
54 | $fields = array_diff($fields, $metadata->identifier); |
||
55 | } |
||
56 | |||
57 | foreach ($metadata->associationMappings as $fieldName => $relation) { |
||
58 | if ($relation['type'] !== ClassMetadata::ONE_TO_MANY) { |
||
59 | $fields[] = $fieldName; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | return $fields; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Prepend the string in the file |
||
68 | * |
||
69 | * @param string $string Text to be added in front of the file |
||
70 | * @param string $filename File to prepend in |
||
71 | */ |
||
72 | View Code Duplication | public static function prepend($string, $filename) |
|
73 | { |
||
74 | $context = stream_context_create(); |
||
75 | $fp = fopen($filename, 'r', 1, $context); |
||
76 | $tmpname = md5($string); |
||
77 | file_put_contents($tmpname, $string); |
||
78 | file_put_contents($tmpname, $fp, FILE_APPEND); |
||
79 | fclose($fp); |
||
80 | unlink($filename); |
||
81 | rename($tmpname, $filename); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Append the string in the file |
||
86 | * |
||
87 | * @param string $string Text to be added in front of the file |
||
88 | * @param string $filename File to prepend in |
||
89 | */ |
||
90 | View Code Duplication | public static function append($string, $filename) |
|
0 ignored issues
–
show
|
|||
91 | { |
||
92 | $context = stream_context_create(); |
||
93 | $fp = fopen($filename, 'r', 1, $context); |
||
94 | $tmpname = md5($string); |
||
95 | file_put_contents($tmpname, $fp); |
||
96 | file_put_contents($tmpname, $string, FILE_APPEND); |
||
97 | fclose($fp); |
||
98 | unlink($filename); |
||
99 | rename($tmpname, $filename); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Find and replace the string in the file |
||
104 | * |
||
105 | * @param string $toReplace Text to be replaced |
||
106 | * @param string $replaceText Text as replacement |
||
107 | * @param string $filename File to replace in |
||
108 | */ |
||
109 | public static function replace($toReplace, $replaceText, $filename) |
||
110 | { |
||
111 | $content = file_get_contents($filename); |
||
112 | if ($content) { |
||
113 | $content = str_replace($toReplace, $replaceText, $content); |
||
114 | file_put_contents($filename, $content); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | 1 | public static function getFullSkeletonPath($pathInSkeleton) |
|
119 | { |
||
120 | 1 | $pathInSkeleton = trim($pathInSkeleton); |
|
121 | |||
122 | // pathInSkeleton needs to be prepended by a / |
||
123 | 1 | if (substr($pathInSkeleton, 0, 1) !== '/') { |
|
124 | $pathInSkeleton = '/' . $pathInSkeleton; |
||
125 | } |
||
126 | |||
127 | // Can't have a / at the end. |
||
128 | 1 | if (substr($pathInSkeleton, -1) == '/') { |
|
129 | $pathInSkeleton = rtrim($pathInSkeleton, '/'); |
||
130 | } |
||
131 | |||
132 | 1 | return __DIR__ . '/../Resources/SensioGeneratorBundle/skeleton' . $pathInSkeleton; |
|
133 | } |
||
134 | |||
135 | public static function ensureOptionsProvided(InputInterface $input, array $options) |
||
136 | { |
||
137 | foreach ($options as $option) { |
||
138 | if (null === $input->getOption($option)) { |
||
139 | throw new \RuntimeException(sprintf('The "%s" option must be provided.', $option)); |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Replaces '\' with '/'. |
||
146 | * |
||
147 | * @param $namespace |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | public static function fixNamespace($namespace) |
||
152 | { |
||
153 | return str_replace('\\', '/', $namespace); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Returns an inputAssistant. |
||
158 | * |
||
159 | * This probably isn't the cleanest way. It'd be nicer if we could make a KunstmaanGenerator class |
||
160 | * which all generators inherit from. It then provides a bunch of helper functions and a uniform manner |
||
161 | * in which the input options are handled. |
||
162 | * |
||
163 | * @param InputInterface $input |
||
164 | * @param OutputInterface $output |
||
165 | * @param QuestionHelper $questionHelper |
||
166 | * @param Kernel $kernel |
||
167 | * @param ContainerInterface $container |
||
168 | * |
||
169 | * @return InputAssistant |
||
170 | */ |
||
171 | public static function getInputAssistant(InputInterface &$input, OutputInterface $output, QuestionHelper $questionHelper, Kernel $kernel, ContainerInterface $container) |
||
172 | { |
||
173 | return new InputAssistant($input, $output, $questionHelper, $kernel, $container); |
||
174 | } |
||
175 | } |
||
176 |
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.