Conditions | 28 |
Paths | 276 |
Total Lines | 116 |
Code Lines | 88 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 1 | 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 |
||
60 | public function execute(InputInterface $input, OutputInterface $output): int |
||
61 | { |
||
62 | $pattern = '/'.$input->getOption('pattern').'/'; |
||
63 | /** @var EntityManager $em */ |
||
64 | $em = $this->getContainer()->get('doctrine')->getManager(); |
||
65 | $entities = array_filter($em->getConfiguration()->getMetadataDriverImpl()->getAllClassNames(), function ($entity) use ($pattern) { |
||
66 | return preg_match($pattern, $entity); |
||
67 | }); |
||
68 | $graph = new Graph(); |
||
69 | $graph->createAttribute('rankdir', 'LR'); |
||
70 | $graph->createAttribute('ranksep', '3'); |
||
71 | /** @var Vertex[] $tables */ |
||
72 | $tables = []; |
||
73 | foreach ($entities as $entity) { |
||
74 | $metadata = $em->getClassMetadata($entity); |
||
75 | if ($metadata->getFieldNames()) { |
||
76 | $table = $graph->createVertex($metadata->getTableName()); |
||
77 | $table->createAttribute('shape', 'record'); |
||
78 | $table->createAttribute('width', '4'); |
||
79 | foreach ($metadata->getFieldNames() as $fieldName) { |
||
80 | $fieldMapping = $metadata->getFieldMapping($fieldName); |
||
81 | $table->addRecord(new Record($this->getFieldMappingDisplayName($fieldMapping))); |
||
82 | } |
||
83 | $tables[$entity] = $table; |
||
84 | } |
||
85 | } |
||
86 | foreach ($entities as $entity) { |
||
87 | $metadata = $em->getClassMetadata($entity); |
||
88 | foreach ($metadata->getAssociationMappings() as $associationMapping) { |
||
89 | if (array_key_exists('joinTable', $associationMapping) && $associationMapping['joinTable']) { |
||
90 | $joinTable = $associationMapping['joinTable']; |
||
91 | $table = $graph->createVertex($joinTable['name']); |
||
92 | $table->createAttribute('shape', 'record'); |
||
93 | $table->createAttribute('width', '4'); |
||
94 | if (array_key_exists('joinColumns', $joinTable)) { |
||
95 | $sourceEntity = $associationMapping['sourceEntity']; |
||
96 | $joinColumns = $joinTable['joinColumns']; |
||
97 | foreach ($joinColumns as $joinColumn) { |
||
98 | $record = new Record($this->getFieldMappingDisplayName($joinColumn, 'name')); |
||
99 | $table->addRecord($record); |
||
100 | $nullable = false; |
||
101 | if (array_key_exists($sourceEntity, $tables)) { |
||
102 | $name = $this->getFieldMappingDisplayName($joinColumn, 'referencedColumnName'); |
||
103 | $nullable = $joinColumn['nullable']; |
||
104 | $tables[$sourceEntity]->addRecord($to = $graph->getVertex($tables[$sourceEntity]->getId())->getRecord($name) ?: new Record($name)); |
||
105 | } |
||
106 | $edge = $record->addEdgeTo($to); |
||
|
|||
107 | $edge->createAttribute('headlabel', $nullable ? '0..1' : '1'); |
||
108 | $edge->createAttribute('taillabel', '*'); |
||
109 | } |
||
110 | } |
||
111 | if (array_key_exists('inverseJoinColumns', $joinTable)) { |
||
112 | $targetEntity = $associationMapping['targetEntity']; |
||
113 | $inverseJoinColumns = $joinTable['inverseJoinColumns']; |
||
114 | foreach ($inverseJoinColumns as $inverseJoinColumn) { |
||
115 | $record = new Record($this->getFieldMappingDisplayName($inverseJoinColumn, 'name')); |
||
116 | $table->addRecord($record); |
||
117 | $nullable = false; |
||
118 | if (array_key_exists($targetEntity, $tables)) { |
||
119 | $name = $this->getFieldMappingDisplayName($inverseJoinColumn, 'referencedColumnName'); |
||
120 | $nullable = $inverseJoinColumn['nullable']; |
||
121 | $tables[$targetEntity]->addRecord($to = $graph->getVertex($tables[$targetEntity]->getId())->getRecord($name) ?: new Record($name)); |
||
122 | } |
||
123 | $edge = $record->addEdgeTo($to); |
||
124 | $edge->createAttribute('headlabel', $nullable ? '0..1' : '1'); |
||
125 | $edge->createAttribute('taillabel', '*'); |
||
126 | } |
||
127 | } |
||
128 | $tables[$table->getId()] = $table; |
||
129 | } else { |
||
130 | $targetEntity = $associationMapping['targetEntity']; |
||
131 | if (!array_key_exists($targetEntity, $tables) || !array_key_exists('sourceToTargetKeyColumns', $associationMapping)) { |
||
132 | continue; |
||
133 | } |
||
134 | $columns = $associationMapping['sourceToTargetKeyColumns']; |
||
135 | $to = $graph->getVertex($tables[$targetEntity]->getId())->getRecord(array_values($columns)[0]); |
||
136 | if (!$to) { |
||
137 | $to = new Record($this->getFieldMappingDisplayName([ |
||
138 | 'columnName' => array_values($columns)[0], |
||
139 | ])); |
||
140 | $tables[$targetEntity]->addRecord($to); |
||
141 | } |
||
142 | $from = $graph->getVertex($tables[$entity]->getId())->getRecord(array_keys($columns)[0]); |
||
143 | if (!$from) { |
||
144 | $from = new Record($this->getFieldMappingDisplayName([ |
||
145 | 'columnName' => array_keys($columns)[0], |
||
146 | ])); |
||
147 | $tables[$entity]->addRecord($from); |
||
148 | } |
||
149 | $joinColumn = $associationMapping['joinColumns'][0]; |
||
150 | $nullable = !array_key_exists('nullable', $joinColumn) || $joinColumn['nullable']; |
||
151 | $edge = $from->addEdgeTo($to); |
||
152 | $edge->createAttribute('headlabel', $nullable ? '0..1' : '1'); |
||
153 | if (array_key_exists('type', $associationMapping)) { |
||
154 | $edge->createAttribute('taillabel', ClassMetadataInfo::ONE_TO_ONE === $associationMapping['type'] ? '1' : '*'); |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | ksort($tables); |
||
160 | $format = $input->getOption('format', 'png'); |
||
161 | $binary = $input->getOption('binary', null); |
||
162 | $path = $input->getOption('output-path', null); |
||
163 | $graphviz = new Graphviz($format, $binary); |
||
164 | if ($path) { |
||
165 | return (int) !file_put_contents($path, $graphviz->createImageData($graph)); |
||
166 | } |
||
167 | if ('dot' === $format) { |
||
168 | $output->writeln((string) $graph); |
||
169 | |||
170 | return 0; |
||
171 | } |
||
172 | // @codeCoverageIgnoreStart |
||
173 | $graphviz->display($graph); |
||
174 | |||
175 | return 0; |
||
176 | // @codeCoverageIgnoreEnd |
||
195 |