| Conditions | 10 |
| Paths | 149 |
| Total Lines | 59 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 10 | ||
| 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 |
||
| 64 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 65 | { |
||
| 66 | /* @var $entityManager \Doctrine\ORM\EntityManager */ |
||
| 67 | $entityManager = $this->getHelper('em')->getEntityManager(); |
||
|
|
|||
| 68 | $schema = $entityManager->getConnection()->getSchemaManager(); |
||
| 69 | |||
| 70 | $classNames = $entityManager->getConfiguration()->getMetadataDriverImpl()->getAllClassNames(); |
||
| 71 | |||
| 72 | if (!$classNames) { |
||
| 73 | throw new \Exception( |
||
| 74 | 'You do not have any mapped Doctrine ORM entities according to the current configuration. '. |
||
| 75 | 'If you have entities or mapping files you should check your mapping configuration for errors.' |
||
| 76 | ); |
||
| 77 | } |
||
| 78 | |||
| 79 | $output->writeln(sprintf("Found <info>%d</info> mapped entities:", count($classNames))); |
||
| 80 | |||
| 81 | $tableNames = array(); |
||
| 82 | foreach($classNames as $className) { |
||
| 83 | $tableNames[$entityManager->getClassMetadata($className)->getTableName()] = $className; |
||
| 84 | } |
||
| 85 | $failure = false; |
||
| 86 | $entity = array(); |
||
| 87 | foreach ($classNames as $className) { |
||
| 88 | try { |
||
| 89 | $tableName = array_search($className, $tableNames); |
||
| 90 | $keys = $schema->listTableForeignKeys($tableName); |
||
| 91 | $output->writeln('<info>'.$className.'('.$tableName.')'.'</info> '.count($keys).' foreign key(s)'); |
||
| 92 | $assoc = $entityManager->getClassMetadata($className)->getAssociationMappings(); |
||
| 93 | foreach($assoc as $a) { |
||
| 94 | if(array_key_exists('sourceToTargetKeyColumns', $a)) { |
||
| 95 | $output->writeln('-> Entity: '.$a['targetEntity'].'('.array_search($a['targetEntity'], $tableNames).')'); |
||
| 96 | $sourceFields = array(); |
||
| 97 | $sourceColumns = array(); |
||
| 98 | foreach ($a['joinColumnFieldNames'] as $column => $field) { |
||
| 99 | $output->writeln("\tField: $field($column)"); |
||
| 100 | $sourceFields[] = $field; |
||
| 101 | $sourceColumns[] = $column; |
||
| 102 | } |
||
| 103 | $targetFields = array(); |
||
| 104 | $targetColumns = array(); |
||
| 105 | foreach($a['sourceToTargetKeyColumns'] as $fcolumn => $ffield) { |
||
| 106 | $output->writeln("\t\tForeign key: $ffield($fcolumn)"); |
||
| 107 | $targetFields[] = $ffield; |
||
| 108 | $targetColumns[] = $fcolumn; |
||
| 109 | } |
||
| 110 | $missing = $this->findMissingEntities($a, $output); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } catch (MappingException $e) { |
||
| 114 | $output->writeln("<error>[FAIL]</error> ".$className); |
||
| 115 | $output->writeln(sprintf("<comment>%s</comment>", $e->getMessage())); |
||
| 116 | $output->writeln(''); |
||
| 117 | |||
| 118 | $failure = true; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | return $failure ? 1 : 0; |
||
| 122 | } |
||
| 123 | |||
| 171 |
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 implementation 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 interface: