Conditions | 12 |
Paths | 292 |
Total Lines | 76 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |
||
85 | protected function execute(InputInterface $input, OutputInterface $output) |
||
86 | { |
||
87 | $type = $input->getArgument('mapping-type') ?: 'xml'; |
||
88 | if ($type === 'yaml') { |
||
89 | $type = 'yml'; |
||
90 | } |
||
91 | |||
92 | $namespaceOrBundle = $input->getArgument('name'); |
||
93 | if (isset($this->bundles[$namespaceOrBundle])) { |
||
94 | $bundle = $this->getApplication()->getKernel()->getBundle($namespaceOrBundle); |
||
|
|||
95 | $namespace = $bundle->getNamespace() . '\Entity'; |
||
96 | |||
97 | $destPath = $bundle->getPath(); |
||
98 | if ($type === 'annotation') { |
||
99 | $destPath .= '/Entity'; |
||
100 | } else { |
||
101 | $destPath .= '/Resources/config/doctrine'; |
||
102 | } |
||
103 | } else { |
||
104 | // assume a namespace has been passed |
||
105 | $namespace = $namespaceOrBundle; |
||
106 | $destPath = $input->getOption('path'); |
||
107 | if ($destPath === null) { |
||
108 | throw new InvalidArgumentException('The --path option is required when passing a namespace (e.g. --path=src). If you intended to pass a bundle name, check your spelling.'); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | $cme = new ClassMetadataExporter(); |
||
113 | $exporter = $cme->getExporter($type); |
||
114 | $exporter->setOverwriteExistingFiles($input->getOption('force')); |
||
115 | |||
116 | if ($type === 'annotation') { |
||
117 | $entityGenerator = $this->getEntityGenerator(); |
||
118 | $exporter->setEntityGenerator($entityGenerator); |
||
119 | } |
||
120 | |||
121 | $em = $this->getEntityManager($input->getOption('em'), $input->getOption('shard')); |
||
122 | |||
123 | $databaseDriver = new DatabaseDriver($em->getConnection()->getSchemaManager()); |
||
124 | $em->getConfiguration()->setMetadataDriverImpl($databaseDriver); |
||
125 | |||
126 | $emName = $input->getOption('em'); |
||
127 | $emName = $emName ? $emName : 'default'; |
||
128 | |||
129 | $cmf = new DisconnectedClassMetadataFactory(); |
||
130 | $cmf->setEntityManager($em); |
||
131 | $metadata = $cmf->getAllMetadata(); |
||
132 | $metadata = MetadataFilter::filter($metadata, $input->getOption('filter')); |
||
133 | if ($metadata) { |
||
134 | $output->writeln(sprintf('Importing mapping information from "<info>%s</info>" entity manager', $emName)); |
||
135 | foreach ($metadata as $class) { |
||
136 | $className = $class->name; |
||
137 | $class->name = $namespace . '\\' . $className; |
||
138 | if ($type === 'annotation') { |
||
139 | $path = $destPath . '/' . str_replace('\\', '.', $className) . '.php'; |
||
140 | } else { |
||
141 | $path = $destPath . '/' . str_replace('\\', '.', $className) . '.orm.' . $type; |
||
142 | } |
||
143 | $output->writeln(sprintf(' > writing <comment>%s</comment>', $path)); |
||
144 | $code = $exporter->exportClassMetadata($class); |
||
145 | $dir = dirname($path); |
||
146 | if (! is_dir($dir)) { |
||
147 | mkdir($dir, 0775, true); |
||
148 | } |
||
149 | file_put_contents($path, $code); |
||
150 | chmod($path, 0664); |
||
151 | } |
||
152 | |||
153 | return 0; |
||
154 | } |
||
155 | |||
156 | $output->writeln('Database does not have any mapping information.'); |
||
157 | $output->writeln(''); |
||
158 | |||
159 | return 1; |
||
160 | } |
||
161 | } |
||
162 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: