| 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 |
||
| 87 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 88 | { |
||
| 89 | $type = $input->getArgument('mapping-type') ?: 'xml'; |
||
| 90 | if ($type === 'yaml') { |
||
| 91 | $type = 'yml'; |
||
| 92 | } |
||
| 93 | |||
| 94 | $namespaceOrBundle = $input->getArgument('name'); |
||
| 95 | if (isset($this->bundles[$namespaceOrBundle])) { |
||
| 96 | $bundle = $this->getApplication()->getKernel()->getBundle($namespaceOrBundle); |
||
|
|
|||
| 97 | $namespace = $bundle->getNamespace() . '\Entity'; |
||
| 98 | |||
| 99 | $destPath = $bundle->getPath(); |
||
| 100 | if ($type === 'annotation') { |
||
| 101 | $destPath .= '/Entity'; |
||
| 102 | } else { |
||
| 103 | $destPath .= '/Resources/config/doctrine'; |
||
| 104 | } |
||
| 105 | } else { |
||
| 106 | // assume a namespace has been passed |
||
| 107 | $namespace = $namespaceOrBundle; |
||
| 108 | $destPath = $input->getOption('path'); |
||
| 109 | if ($destPath === null) { |
||
| 110 | 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.'); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | $cme = new ClassMetadataExporter(); |
||
| 115 | $exporter = $cme->getExporter($type); |
||
| 116 | $exporter->setOverwriteExistingFiles($input->getOption('force')); |
||
| 117 | |||
| 118 | if ($type === 'annotation') { |
||
| 119 | $entityGenerator = $this->getEntityGenerator(); |
||
| 120 | $exporter->setEntityGenerator($entityGenerator); |
||
| 121 | } |
||
| 122 | |||
| 123 | $em = $this->getEntityManager($input->getOption('em'), $input->getOption('shard')); |
||
| 124 | |||
| 125 | $databaseDriver = new DatabaseDriver($em->getConnection()->getSchemaManager()); |
||
| 126 | $em->getConfiguration()->setMetadataDriverImpl($databaseDriver); |
||
| 127 | |||
| 128 | $emName = $input->getOption('em'); |
||
| 129 | $emName = $emName ? $emName : 'default'; |
||
| 130 | |||
| 131 | $cmf = new DisconnectedClassMetadataFactory(); |
||
| 132 | $cmf->setEntityManager($em); |
||
| 133 | $metadata = $cmf->getAllMetadata(); |
||
| 134 | $metadata = MetadataFilter::filter($metadata, $input->getOption('filter')); |
||
| 135 | if ($metadata) { |
||
| 136 | $output->writeln(sprintf('Importing mapping information from "<info>%s</info>" entity manager', $emName)); |
||
| 137 | foreach ($metadata as $class) { |
||
| 138 | $className = $class->name; |
||
| 139 | $class->name = $namespace . '\\' . $className; |
||
| 140 | if ($type === 'annotation') { |
||
| 141 | $path = $destPath . '/' . str_replace('\\', '.', $className) . '.php'; |
||
| 142 | } else { |
||
| 143 | $path = $destPath . '/' . str_replace('\\', '.', $className) . '.orm.' . $type; |
||
| 144 | } |
||
| 145 | $output->writeln(sprintf(' > writing <comment>%s</comment>', $path)); |
||
| 146 | $code = $exporter->exportClassMetadata($class); |
||
| 147 | $dir = dirname($path); |
||
| 148 | if (! is_dir($dir)) { |
||
| 149 | mkdir($dir, 0775, true); |
||
| 150 | } |
||
| 151 | file_put_contents($path, $code); |
||
| 152 | chmod($path, 0664); |
||
| 153 | } |
||
| 154 | |||
| 155 | return 0; |
||
| 156 | } |
||
| 157 | |||
| 158 | $output->writeln('Database does not have any mapping information.'); |
||
| 159 | $output->writeln(''); |
||
| 160 | |||
| 161 | return 1; |
||
| 162 | } |
||
| 163 | } |
||
| 164 |
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: