| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This file is part of phpDocumentor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @copyright 2010-2018 Mike van Riel<[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @license   http://www.opensource.org/licenses/mit-license.php MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @link      http://phpdoc.org | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace phpDocumentor\Reflection\Php\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use phpDocumentor\Reflection\DocBlock as DocBlockDescriptor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use phpDocumentor\Reflection\DocBlockFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use phpDocumentor\Reflection\Location; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use phpDocumentor\Reflection\Php\ProjectFactoryStrategy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use phpDocumentor\Reflection\Php\StrategyContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use phpDocumentor\Reflection\Types\Context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use PhpParser\Comment\Doc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * Strategy as wrapper around the DocBlockFactoryInterface. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @see DocBlockFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @see DocBlockDescriptor | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | final class DocBlock implements ProjectFactoryStrategy | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * Wrapped DocBlock factory | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @var DocBlockFactoryInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     private $docblockFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * Initializes the object with a DocBlockFactory implementation. | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |     public function __construct(DocBlockFactoryInterface $docBlockFactory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |         $this->docblockFactory = $docBlockFactory; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |     public function matches($object): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |         return $object instanceof Doc; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * Creates an Element out of the given object. | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * Since an object might contain other objects that need to be converted the $factory is passed so it can be | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * used to create nested Elements. | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @param Doc $object object to convert to an Element | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * @param StrategyContainer $strategies used to convert nested objects. | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * @param Context $context of the created object | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @return null|DocBlockDescriptor | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |     public function create($object, StrategyContainer $strategies, ?Context $context = null): ?DocBlockDescriptor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 3 |  |         if ($object === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |         if (!$this->matches($object)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |             throw new InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |                     '%s cannot handle objects with the type %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |                     __CLASS__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |                     is_object($object) ? get_class($object) : gettype($object) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |         return $this->docblockFactory->create($object->getText(), $context, new Location($object->getLine())); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 80 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |  | 
            
                        
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.