| Conditions | 12 |
| Paths | 292 |
| Total Lines | 75 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 68 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 69 | { |
||
| 70 | $type = $input->getArgument('mapping-type') ? $input->getArgument('mapping-type') : 'xml'; |
||
| 71 | if ($type === 'yaml') { |
||
| 72 | $type = 'yml'; |
||
| 73 | } |
||
| 74 | |||
| 75 | $bundles = $this->getContainer()->getParameter('kernel.bundles'); |
||
| 76 | if (isset($bundles[$input->getArgument('name')])) { |
||
| 77 | $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name')); |
||
|
|
|||
| 78 | $namespace = $bundle->getNamespace(). '\\Entity'; |
||
| 79 | |||
| 80 | $destPath = $bundle->getPath(); |
||
| 81 | if ($type === 'annotation') { |
||
| 82 | $destPath .= '/Entity'; |
||
| 83 | } else { |
||
| 84 | $destPath .= '/Resources/config/doctrine'; |
||
| 85 | } |
||
| 86 | } else { |
||
| 87 | // assume a namespace has been passed |
||
| 88 | $namespace = $input->getArgument('name'); |
||
| 89 | if (null === $destPath = $input->getOption('path')) { |
||
| 90 | 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.'); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | $cme = new ClassMetadataExporter(); |
||
| 95 | $exporter = $cme->getExporter($type); |
||
| 96 | $exporter->setOverwriteExistingFiles($input->getOption('force')); |
||
| 97 | |||
| 98 | if ($type === 'annotation') { |
||
| 99 | $entityGenerator = $this->getEntityGenerator(); |
||
| 100 | $exporter->setEntityGenerator($entityGenerator); |
||
| 101 | } |
||
| 102 | |||
| 103 | $em = $this->getEntityManager($input->getOption('em'), $input->getOption('shard')); |
||
| 104 | |||
| 105 | $databaseDriver = new DatabaseDriver($em->getConnection()->getSchemaManager()); |
||
| 106 | $em->getConfiguration()->setMetadataDriverImpl($databaseDriver); |
||
| 107 | |||
| 108 | $emName = $input->getOption('em'); |
||
| 109 | $emName = $emName ? $emName : 'default'; |
||
| 110 | |||
| 111 | $cmf = new DisconnectedClassMetadataFactory(); |
||
| 112 | $cmf->setEntityManager($em); |
||
| 113 | $metadata = $cmf->getAllMetadata(); |
||
| 114 | $metadata = MetadataFilter::filter($metadata, $input->getOption('filter')); |
||
| 115 | if ($metadata) { |
||
| 116 | $output->writeln(sprintf('Importing mapping information from "<info>%s</info>" entity manager', $emName)); |
||
| 117 | foreach ($metadata as $class) { |
||
| 118 | $className = $class->name; |
||
| 119 | $class->name = $namespace . '\\' . $className; |
||
| 120 | if ($type === 'annotation') { |
||
| 121 | $path = $destPath . '/' . str_replace('\\', '.', $className) . '.php'; |
||
| 122 | } else { |
||
| 123 | $path = $destPath . '/' . str_replace('\\', '.', $className) . '.orm.' . $type; |
||
| 124 | } |
||
| 125 | $output->writeln(sprintf(' > writing <comment>%s</comment>', $path)); |
||
| 126 | $code = $exporter->exportClassMetadata($class); |
||
| 127 | $dir = dirname($path); |
||
| 128 | if (! is_dir($dir)) { |
||
| 129 | mkdir($dir, 0775, true); |
||
| 130 | } |
||
| 131 | file_put_contents($path, $code); |
||
| 132 | chmod($path, 0664); |
||
| 133 | } |
||
| 134 | |||
| 135 | return 0; |
||
| 136 | } else { |
||
| 137 | $output->writeln('Database does not have any mapping information.'); |
||
| 138 | $output->writeln(''); |
||
| 139 | |||
| 140 | return 1; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 |
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: