Conditions | 13 |
Paths | 50 |
Total Lines | 108 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 4 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
33 | public function processMapping( |
||
34 | EntityMappingInterface $mapping, |
||
35 | ClassLoader $loader |
||
36 | ): string|null { |
||
37 | /** @var array<array-key, Column> $columns */ |
||
38 | $columns = $mapping->collectDBALColumns(); |
||
39 | |||
40 | if (empty($columns)) { |
||
41 | return null; |
||
42 | } |
||
43 | |||
44 | /** @var string $fullClassName */ |
||
45 | $fullClassName = $mapping->getEntityClassName(); |
||
46 | |||
47 | /** @var string|false $filePath */ |
||
48 | $filePath = $loader->findFile($fullClassName); |
||
49 | |||
50 | if ($filePath === false) { |
||
51 | return null; |
||
52 | } |
||
53 | |||
54 | /** @var array<string, bool> $walkedFiles */ |
||
55 | $walkedFiles = [$filePath => true]; |
||
56 | |||
57 | /** @var string $safeFilePath */ |
||
58 | $safeFilePath = $filePath; |
||
59 | |||
60 | do { |
||
61 | if (!file_exists($filePath)) { |
||
62 | $filePath = $safeFilePath; |
||
63 | break; |
||
64 | } |
||
65 | |||
66 | /** @var string $entityPHP */ |
||
67 | $entityPHP = file_get_contents($filePath); |
||
68 | |||
69 | if (1 === preg_match("#\/\*\* \@addiks-original-file ([^\*]*) \*\/#is", $entityPHP, $matches)) { |
||
70 | $safeFilePath = $filePath; |
||
71 | $filePath = trim($matches[1]); |
||
72 | |||
73 | if (!file_exists($filePath)) { |
||
74 | /** @var string $relativePath */ |
||
75 | $relativePath = dirname($safeFilePath) . '/' . $filePath; |
||
76 | |||
77 | if (file_exists($relativePath)) { |
||
78 | $filePath = realpath($relativePath); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | if (isset($walkedFiles[$filePath])) { |
||
83 | break; # Circular reference detected |
||
84 | } |
||
85 | $walkedFiles[$filePath] = true; |
||
86 | continue; |
||
87 | } |
||
88 | break; |
||
89 | } while (true); |
||
90 | |||
91 | /** @var int $classStartPosition */ |
||
92 | $classStartPosition = self::findClassStartPosition($fullClassName, $entityPHP); |
||
|
|||
93 | |||
94 | /** @var array<string, Column> $writtenFieldNames */ |
||
95 | $writtenFieldNames = array(); |
||
96 | |||
97 | foreach ($columns as $column) { |
||
98 | |||
99 | /** @var string $fieldName */ |
||
100 | $fieldName = $this->dataLoader->columnToFieldName($column); |
||
101 | |||
102 | /** @var string $fieldPHP */ |
||
103 | $fieldPHP = sprintf( |
||
104 | "\n%spublic $%s;\n", |
||
105 | $this->indenting, |
||
106 | $fieldName |
||
107 | ); |
||
108 | |||
109 | if (isset($writtenFieldNames[$fieldName]) || str_contains($entityPHP, $fieldPHP)) { |
||
110 | continue; |
||
111 | } |
||
112 | |||
113 | $writtenFieldNames[$fieldName] = $column; |
||
114 | |||
115 | $entityPHP = sprintf( |
||
116 | '%s%s%s', |
||
117 | substr($entityPHP, 0, $classStartPosition), |
||
118 | $fieldPHP, |
||
119 | substr($entityPHP, $classStartPosition) |
||
120 | ); |
||
121 | } |
||
122 | |||
123 | $targetFilePath = sprintf( |
||
124 | '%s/%s.php', |
||
125 | $this->targetDirectory, |
||
126 | str_replace('\\', '_', $fullClassName) |
||
127 | ); |
||
128 | |||
129 | $entityPHP .= sprintf( |
||
130 | "\n\n/** @addiks-original-file %s */\n", |
||
131 | self::relativePathFromTo($targetFilePath, realpath($filePath)) |
||
132 | ); |
||
133 | |||
134 | if (!is_dir($this->targetDirectory)) { |
||
135 | mkdir($this->targetDirectory, 0777, true); |
||
136 | } |
||
137 | |||
138 | file_put_contents($targetFilePath, $entityPHP); |
||
139 | |||
140 | return $targetFilePath; |
||
141 | } |
||
255 |