| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of Zenify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace ZenifyCodingStandard\Sniffs\Commenting; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use PHP_CodeSniffer_File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use PHP_CodeSniffer_Standards_AbstractVariableSniff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Rules: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * - Property should have docblock comment (except for {@inheritdoc}). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @see PHP_CodeSniffer_Standards_AbstractVariableSniff is used, because it's very difficult to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * separate properties from variables (in args, method etc.). This class does is for us. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | final class VarPropertyCommentSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	const NAME = 'ZenifyCodingStandard.Commenting.VarPropertyComment'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * @param PHP_CodeSniffer_File $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * @param int $position | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  | 	protected function processMemberVar(PHP_CodeSniffer_File $file, $position) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  | 		$commentString = $this->getPropertyComment($file, $position); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  | 		if (strpos($commentString, '@var') !== FALSE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  | 		$file->addError('Property should have docblock comment.', $position); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * @param PHP_CodeSniffer_File $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @param int $position | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  | 	protected function processVariable(PHP_CodeSniffer_File $file, $position) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 * @param PHP_CodeSniffer_File $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * @param int $position | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	protected function processVariableInString(PHP_CodeSniffer_File $file, $position) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 1 |  | 	private function getPropertyComment(PHP_CodeSniffer_File $file, int $position) : string | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 1 |  | 		$commentEnd = $file->findPrevious([T_DOC_COMMENT_CLOSE_TAG], $position); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 1 |  | 		if ($commentEnd === FALSE) { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 1 |  | 			return ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 1 |  | 		$tokens = $file->getTokens(); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 1 |  | 		if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 			return ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 		} else { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 			// Make sure the comment we have found belongs to us. | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 1 |  | 			$commentFor = $file->findNext(T_VARIABLE, $commentEnd + 1); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 1 |  | 			if ($commentFor !== $position) { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 1 |  | 				return ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 1 |  | 		$commentStart = $file->findPrevious(T_DOC_COMMENT_OPEN_TAG, $position); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 1 |  | 		return $file->getTokensAsString($commentStart, $commentEnd - $commentStart + 1); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  | 
            
                        
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.