| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of Railt package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace Railt\SDL\Compiler\Builder\Invocation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Railt\Parser\Ast\RuleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Railt\Reflection\Contracts\Definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Railt\Reflection\Definition\DirectiveDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Railt\Reflection\Invocation\DirectiveInvocation as Invocation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Railt\SDL\Compiler\Ast\Invocation\DirectiveArgumentNode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Railt\SDL\Compiler\Ast\Invocation\DirectiveNode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Railt\SDL\Compiler\Builder\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Railt\SDL\Exception\TypeConflictException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Class DirectiveInvocationBuilder | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | class DirectiveBuilder extends Builder | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @param RuleInterface|DirectiveNode $rule | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @param Definition $parent | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @return Definition | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function build(RuleInterface $rule, Definition $parent): Definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $directive = new Invocation($parent->getDocument(), $rule->getDirectiveName()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $directive->withOffset($rule->getOffset()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->when->resolving(function () use ($rule, $directive): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $definition = $this->loadDefinition($directive, $rule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $this->buildArguments($directive, $definition, $rule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $this->validateUncompletedArguments($directive, $definition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         return $directive; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @param Invocation $directive | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @param RuleInterface $rule | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @return DirectiveDefinition | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * @throws \Railt\Io\Exception\ExternalFileException | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     private function loadDefinition(Invocation $directive, RuleInterface $rule): DirectiveDefinition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         /** @var DirectiveDefinition $definition */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $definition = $directive->getDefinition(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 58 |  | View Code Duplication |         if (! ($definition instanceof Definition\DirectiveDefinition)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $error = '%s should be a Directive, but %s given'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             throw (new TypeConflictException(\sprintf($error, $directive, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 $definition)))->throwsIn($directive->getFile(), $rule->getOffset()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return $definition; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * @param Invocation $call | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * @param DirectiveDefinition $def | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * @param RuleInterface|DirectiveNode $rule | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * @throws \Railt\Io\Exception\ExternalFileException | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     private function buildArguments(Invocation $call, DirectiveDefinition $def, RuleInterface $rule): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         /** @var DirectiveArgumentNode $ast */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         foreach ($rule->getDirectiveArguments() as $ast) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $name = $ast->getArgumentName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $argument = $def->getArgument($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 80 |  | View Code Duplication |             if ($argument === null) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $error = 'Directive %s does not provide argument %s'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 throw (new TypeConflictException(\sprintf($error, $def, $name)))->throwsIn($call->getFile(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                         $ast->getOffset()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $value = $this->valueOf($argument, $ast->getArgumentValue()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $call->withArgument($name, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * @param Invocation $directive | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * @param DirectiveDefinition $definition | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * @throws \Railt\Io\Exception\ExternalFileException | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     private function validateUncompletedArguments(Invocation $directive, DirectiveDefinition $definition): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         foreach ($definition->getArguments() as $argument) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             if ($directive->hasArgument($argument->getName())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             if (! $argument->hasDefaultValue()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 $error = 'Missing value for required argument %s of %s'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 throw (new TypeConflictException(\sprintf($error, $argument, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                     $directive)))->throwsIn($directive->getFile(), $directive->getLine(), $directive->getColumn()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |  | 
            
                        
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: