| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Doctrine\ORM\Tools\Console\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\ORM\Tools\SchemaValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Style\SymfonyStyle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Command to validate that the current mapping is valid. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class ValidateSchemaCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     protected function configure() | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         $this->setName('orm:validate-schema') | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |              ->setDescription('Validate the mapping files') | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |              ->addOption('skip-mapping', null, InputOption::VALUE_NONE, 'Skip the mapping validation check') | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |              ->addOption('skip-sync', null, InputOption::VALUE_NONE, 'Skip checking if the mapping is in sync with the database') | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |              ->setHelp('Validate that the mapping files are correct and in sync with the database.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $ui          = new SymfonyStyle($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $em          = $this->getHelper('em')->getEntityManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $validator   = new SchemaValidator($em); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $exit        = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $skipMapping = (bool) $input->getOption('skip-mapping'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $ui->section('Mapping'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if ($skipMapping) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $ui->text('<comment>[SKIPPED] The mapping was not checked.</comment>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if (! $skipMapping) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $errors = $validator->validateMapping(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             if ($errors) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 foreach ($errors as $className => $errorMessages) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                     $ui->text( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                         sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                             '<error>[FAIL]</error> The entity-class <comment>%s</comment> mapping is invalid:', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                             $className | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     $ui->listing($errorMessages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                     $ui->newLine(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 ++$exit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $ui->success('The mapping files are correct.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $ui->section('Database'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if ($input->getOption('skip-sync')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $ui->text('<comment>[SKIPPED] The database was not checked for synchronicity.</comment>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } elseif (! $validator->schemaInSyncWithMetadata()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $ui->error('The database schema is not in sync with the current mapping file.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $exit += 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $ui->success('The database schema is in sync with the mapping files.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return $exit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                        
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.