Completed
Push — master ( 5feb7e...113385 )
by Mark A.
24s queued 11s
created

CacheInvalidator::setCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
namespace SG\Cache;
4
5
use SG\SemanticDataComparator;
6
use SG\PropertyRegistrationHelper;
7
8
use SMW\Store;
9
use SMW\SemanticData;
10
use SMW\DIWikiPage;
11
use SMW\DIProperty;
12
13
use Lingo\LingoParser;
14
15
use MediaWiki\Linker\LinkTarget;
16
17
/**
18
 * @ingroup SG
19
 * @ingroup SemanticGlossary
20
 *
21
 * @license GNU GPL v2+
22
 * @since 1.0
23
 *
24
 * @author Stephan Gambke
25
 * @author mwjames
26
 */
27
class CacheInvalidator {
28
29
	/**
30
	 * @var CacheInvalidator
31
	 */
32
	private static $instance = null;
33
34
	/**
35
	 * @var GlossaryCache
36
	 */
37
	private $cache = null;
38
39
	/**
40
	 * @since 1.0
41
	 *
42
	 * @return CacheInvalidator
43
	 */
44 4
	public static function getInstance() {
45
46 4
		if ( self::$instance === null ) {
47
48 1
			$instance = new self();
49 1
			$instance->setCache( new GlossaryCache() );
50
51 1
			self::$instance = $instance;
52
		}
53
54 4
		return self::$instance;
55
	}
56
57
	/**
58
	 * @since 1.0
59
	 */
60 1
	public static function clear() {
61 1
		self::$instance = null;
62 1
	}
63
64
	/**
65
	 * @since 1.0
66
	 *
67
	 * @param GlossaryCache $glossaryCache
68
	 */
69 7
	public function setCache( GlossaryCache $glossaryCache ) {
70 7
		$this->glossaryCache = $glossaryCache;
0 ignored issues
show
Bug introduced by
The property glossaryCache does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
71 7
	}
72
73
	/**
74
	 * @since 1.0
75
	 *
76
	 * @param Store $store
77
	 * @param SemanticData $semanticData
78
	 *
79
	 * @return boolean
80
	 */
81 6
	public function invalidateCacheOnStoreUpdate( Store $store, SemanticData $semanticData = null ) {
82
83 6
		if ( $semanticData === null ) {
84 1
			return false;
85
		}
86
87 5
		$this->matchAllSubobjects( $store, $semanticData );
88
89 5
		if ( $this->hasSemanticDataDeviation( $store, $semanticData ) ) {
90 3
			$this->purgeCache( $semanticData->getSubject() );
91 3
			LingoParser::purgeCache();
0 ignored issues
show
Deprecated Code introduced by
The method Lingo\LingoParser::purgeCache() has been deprecated with message: 2.0.2

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
92
		}
93
94 5
		return true;
95
	}
96
97
	/**
98
	 * @since 1.0
99
	 *
100
	 * @param Store $store
101
	 * @param DIWikiPage $subject
102
	 * @param boolean|true $purgeLingo
103
	 *
104
	 * @return boolean
105
	 */
106 3
	public function invalidateCacheOnPageDelete( Store $store, DIWikiPage $subject, $purgeLingo = true ) {
107
108 3
		$this->matchSubobjectsToSubject( $store, $subject );
109 3
		$this->purgeCache( $subject );
110
111 3
		if ( $purgeLingo ) {
112 3
			LingoParser::purgeCache();
0 ignored issues
show
Deprecated Code introduced by
The method Lingo\LingoParser::purgeCache() has been deprecated with message: 2.0.2

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
113
		}
114
115 3
		return true;
116
	}
117
118
	/**
119
	 * @since 1.0
120
	 *
121
	 * @param LinkTarget $title
122
	 *
123
	 * @return boolean
124
	 */
125 1
	public function invalidateCacheOnPageMove( LinkTarget $title ) {
126 1
		$this->purgeCache( DIWikiPage::newFromText( $title->getDBkey(), $title->getNamespace() ));
127 1
		return true;
128
	}
129
130 5
	private function matchAllSubobjects( Store $store, SemanticData $semanticData ) {
131
132 5
		$properties = $semanticData->getProperties();
133
134 5
		if ( array_key_exists( DIProperty::TYPE_SUBOBJECT, $properties ) ) {
135 1
			foreach ( $semanticData->getPropertyValues( $properties[ DIProperty::TYPE_SUBOBJECT ] ) as $subobject ) {
136 1
				$this->invalidateCacheOnStoreUpdate(
137 1
					$store,
138 1
					$semanticData->findSubSemanticData( $subobject->getSubobjectName() ),
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataItem as the method getSubobjectName() does only exist in the following sub-classes of SMWDataItem: SMWDIWikiPage, SMW\DIWikiPage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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 sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
139 1
					false
0 ignored issues
show
Unused Code introduced by
The call to CacheInvalidator::invalidateCacheOnStoreUpdate() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
140
				);
141
			}
142
		}
143 5
	}
144
145 3
	private function matchSubobjectsToSubject( Store $store, DIWikiPage $subject ) {
146
147 3
		$properties = $store->getProperties( $subject );
148
149 3
		if ( array_key_exists( DIProperty::TYPE_SUBOBJECT, $properties ) ) {
150 1
			foreach ( $store->getPropertyValues( $subject, $properties[ DIProperty::TYPE_SUBOBJECT ] ) as $subobject ) {
151
				$this->invalidateCacheOnPageDelete(
152
					$store,
153
					$subobject->getSubject(),
154
					false
155
				);
156
			}
157
		}
158 3
	}
159
160 5
	private function hasSemanticDataDeviation( Store $store, SemanticData $semanticData ) {
161
162 5
		$dataComparator = new SemanticDataComparator( $store, $semanticData );
163
164 5
		return $dataComparator->compareForProperty( PropertyRegistrationHelper::SG_TERM ) ||
165 5
			$dataComparator->compareForProperty( PropertyRegistrationHelper::SG_DEFINITION ) ||
166 5
			$dataComparator->compareForProperty( PropertyRegistrationHelper::SG_LINK ) ||
167 5
			$dataComparator->compareForProperty( PropertyRegistrationHelper::SG_STYLE );
168
	}
169
170 6
	private function purgeCache( DIWikiPage $subject ) {
171
172 6
		$this->glossaryCache->getCache()->delete(
173 6
			$this->glossaryCache->getKeyForSubject( $subject )
174
		);
175
176 6
		return true;
177
	}
178
179
}
180