Completed
Push — master ( 274d0e...9fc64f )
by mw
34:08
created

EventListenerRegistry::addListenersToCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 47
ccs 32
cts 32
cp 1
rs 9.0303
c 1
b 0
f 1
cc 1
eloc 24
nc 1
nop 0
crap 1
1
<?php
2
3
namespace SMW;
4
5
use Onoi\EventDispatcher\EventListenerCollection;
6
use Onoi\EventDispatcher\EventDispatcherFactory;
7
use SMWExporter as Exporter;
8
use SMW\Query\QueryComparator;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 2.2
13
 *
14
 * @author mwjames
15
 */
16
class EventListenerRegistry implements EventListenerCollection {
17
18
	/**
19
	 * @var EventListenerCollection
20
	 */
21
	private $eventListenerCollection = null;
22
23
	/**
24
	 * @since 2.2
25
	 *
26
	 * @param EventListenerCollection $eventListenerCollection
27
	 */
28 3
	public function __construct( EventListenerCollection $eventListenerCollection ) {
29 3
		$this->eventListenerCollection = $eventListenerCollection;
30 3
	}
31
32
	/**
33
	 * @see  EventListenerCollection::getCollection
34
	 *
35
	 * @since 2.2
36
	 */
37 1
	public function getCollection() {
38 1
		return $this->addListenersToCollection()->getCollection();
39
	}
40
41 93
	private function addListenersToCollection() {
42
43 1
		$this->eventListenerCollection->registerCallback(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Onoi\EventDispatcher\EventListenerCollection as the method registerCallback() does only exist in the following implementations of said interface: Onoi\EventDispatcher\Lis...EventListenerCollection.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
44 1
			'factbox.cache.delete', function( $dispatchContext ) {
45
46 29
				$title = $dispatchContext->get( 'title' );
47 29
				$cache = ApplicationFactory::getInstance()->getCache();
48
49 29
				$cache->delete(
50 29
					ApplicationFactory::getInstance()->newCacheFactory()->getFactboxCacheKey( $title->getArticleID() )
51 29
				);
52 29
			}
53 1
		);
54
55 1
		$this->eventListenerCollection->registerCallback(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Onoi\EventDispatcher\EventListenerCollection as the method registerCallback() does only exist in the following implementations of said interface: Onoi\EventDispatcher\Lis...EventListenerCollection.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
56 1
			'exporter.reset', function() {
57 93
				Exporter::getInstance()->clear();
58 93
			}
59 1
		);
60
61 1
		$this->eventListenerCollection->registerCallback(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Onoi\EventDispatcher\EventListenerCollection as the method registerCallback() does only exist in the following implementations of said interface: Onoi\EventDispatcher\Lis...EventListenerCollection.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
62 1
			'query.comparator.reset', function() {
63 91
				QueryComparator::getInstance()->clear();
64 91
			}
65 1
		);
66
67 1
		$this->eventListenerCollection->registerCallback(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Onoi\EventDispatcher\EventListenerCollection as the method registerCallback() does only exist in the following implementations of said interface: Onoi\EventDispatcher\Lis...EventListenerCollection.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
68 93
			'property.spec.change', function( $dispatchContext ) {
69
70 93
				$applicationFactory = ApplicationFactory::getInstance();
71
				$subject = $dispatchContext->get( 'subject' );
72 93
73 93
				$updateDispatcherJob = $applicationFactory->newJobFactory()->newUpdateDispatcherJob(
74 93
					$subject->getTitle()
75
				);
76 93
77
				$updateDispatcherJob->run();
78 93
79
				Exporter::getInstance()->resetCacheFor( $subject );
80 93
				$applicationFactory->getPropertySpecificationLookup()->resetCacheFor( $subject );
81 93
82 1
				$dispatchContext->set( 'propagationstop', true );
83
			}
84 1
		);
85
86
		return $this->eventListenerCollection;
87
	}
88
89
}
90