| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SESP\PropertyAnnotators; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ApprovedRevs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SMW\DIProperty; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SMW\SemanticData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SMWDataItem as DataItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SMWDITime as DITime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SESP\PropertyAnnotator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SESP\AppFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @private | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @ingroup SESP | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @license GNU GPL v2+ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | class ApprovedDatePropertyAnnotator implements PropertyAnnotator { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | 	 * Predefined property ID | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 	const PROP_ID = '___APPROVEDDATE'; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 	 * @var AppFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 	private $appFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 	 * @var Integer|null | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  | 	private $approvedDate; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 	 * @param AppFactory $appFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 4 |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 4 |  | 	public function __construct( AppFactory $appFactory ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 4 |  | 		$this->appFactory = $appFactory; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 	 * @since 2.0 | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 	 * @param Integer $approvedDate | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 2 |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  | 	public function setApprovedDate( $approvedDate ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  | 		$this->approvedDate = $approvedDate; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 	 * {@inheritDoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 1 |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  | 	public function isAnnotatorFor( DIProperty $property ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		return $property->getKey() === self::PROP_ID; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 	 * {@inheritDoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 2 |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	public function addAnnotation( DIProperty $property, SemanticData $semanticData ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		if ( $this->approvedDate === null && class_exists( 'ApprovedRevs' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			$title = $semanticData->getSubject()->getTitle(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			if ( ApprovedRevs::pageIsApprovable( $title ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 				$this->approvedDate = ApprovedRevs::getApprovedTimestamp( $title ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			if ( !$this->approvedDate ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 				$logReader = $this->appFactory->newDatabaseLogReader( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 					$semanticData->getSubject()->getTitle(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 2 |  | 					'approval' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 				); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  | 				$this->approvedDate = $logReader->getDateOfLogEntry(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  | 		$dataItem = $this->getDataItem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  | 		if ( $dataItem ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  | 			$semanticData->addPropertyObjectValue( $property, $dataItem ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  | 			$semanticData->removeProperty( $property ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  | 		} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 | 1 |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  | 	private function getDataItem() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  | 		if ( $this->approvedDate ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  | 			$date = $this->approvedDate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  | 			return new DITime( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 				DITime::CM_GREGORIAN, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  | 				$date->format( 'Y' ), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 				$date->format( 'm' ), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 				$date->format( 'd' ), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 				$date->format( 'H' ), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 				$date->format( 'i' ) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 103 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                        
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.