| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the PHP Translation package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) PHP Translation team <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Translation\Extractor\Visitor\Php\Symfony; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Doctrine\Common\Annotations\AnnotationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use PhpParser\Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use PhpParser\NodeVisitor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Validator\Mapping\ClassMetadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Translation\Extractor\Visitor\Php\BasePHPVisitor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @author Tobias Nyholm <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | final class ValidationAnnotation extends BasePHPVisitor implements NodeVisitor | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @var MetadataFactoryInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     private $metadataFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     private $namespace; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * ValidationExtractor constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * @param MetadataFactoryInterface $metadataFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 2 |  |     public function __construct(MetadataFactoryInterface $metadataFactory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  |         $this->metadataFactory = $metadataFactory; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |     public function beforeTraverse(array $nodes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |         $this->namespace = ''; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |     public function enterNode(Node $node) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 53 | 2 | View Code Duplication |         if ($node instanceof Node\Stmt\Namespace_) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |             if (isset($node->name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 // save the namespace | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |                 $this->namespace = implode('\\', $node->name->parts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |         if (!$node instanceof Node\Stmt\Class_) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |         $name = '' === $this->namespace ? $node->name : $this->namespace.'\\'.$node->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |         if (!class_exists($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             /** @var ClassMetadata $metadata */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |             $metadata = $this->metadataFactory->getMetadataFor($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 2 |  |         } catch (AnnotationException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 2 |  |             $this->addError($node, sprintf('Could not parse class "%s" for annotations. %s', $this->namespace, $e->getMessage())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         if (!$metadata->hasConstraints() && !count($metadata->getConstrainedProperties())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         $this->extractFromConstraints($metadata->constraints); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |         foreach ($metadata->members as $members) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |             foreach ($members as $member) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |                 $this->extractFromConstraints($member->constraints); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * @param array $constraints | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |     private function extractFromConstraints(array $constraints) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |         foreach ($constraints as $constraint) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |             $ref = new \ReflectionClass($constraint); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |             $defaultValues = $ref->getDefaultProperties(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |             $properties = $ref->getProperties(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |             foreach ($properties as $property) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |                 $propName = $property->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 // If the property ends with 'Message' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |                 if ('message' === strtolower(substr($propName, -1 * strlen('Message')))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                     // If it is different from the default value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |                     if ($defaultValues[$propName] !== $constraint->{$propName}) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |                         $this->addLocation($constraint->{$propName}, 0, null, ['domain' => 'validators']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 2 |  |     public function leaveNode(Node $node) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 2 |  |     public function afterTraverse(array $nodes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 | 2 |  |     } | 
            
                                                        
            
                                    
            
            
                | 125 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.