Completed
Pull Request — master (#268)
by
unknown
03:49 queued 02:35
created

Xhgui_WatchFunctions   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 64
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A save() 0 18 5
A getAll() 0 10 2
A truncate() 0 4 1
1
<?php
2
class Xhgui_WatchFunctions
3
{
4
    protected $storage;
5
6
    public function __construct(\Xhgui_StorageInterface $storage)
7
    {
8
        $this->storage = $storage;
9
    }
10
11
    /**
12
     * Save a value to the collection.
13
     *
14
     * Will do an insert or update depending
15
     * on the id field being present.
16
     *
17
     * @param array $data The data to save.
18
     * @return boolean
19
     */
20
    public function save($data)
21
    {
22
        if (empty($data['name'])) {
23
            return false;
24
        }
25
26
        if (!empty($data['removed']) && isset($data['_id'])) {
27
            $this->storage->remove($data['_id']);
28
            return true;
29
        }
30
31
        if (empty($data['_id'])) {
32
            $this->storage->insert($data);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Xhgui_StorageInterface as the method insert() does only exist in the following implementations of said interface: Xhgui_Storage_File, Xhgui_Storage_Mongo.

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...
33
            return true;
34
        }
35
        $this->storage->update($data['_id'], $data);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Xhgui_StorageInterface as the method update() does only exist in the following implementations of said interface: Xhgui_Storage_File, Xhgui_Storage_Mongo.

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...
36
        return true;
37
    }
38
39
    /**
40
     * Get all the known watch functions.
41
     *
42
     * @return array Array of watch functions.
43
     */
44
    public function getAll()
45
    {
46
        $cursor = $this->storage->find();
0 ignored issues
show
Bug introduced by
The call to find() misses a required argument $filter.

This check looks for function calls that miss required arguments.

Loading history...
47
        if ($this->storage instanceof \Xhgui_Storage_Mongo) {
48
            $ret = $cursor->toArray();
49
        } else {
50
            $ret = array_column($cursor->toArray(), 'profile', '_id');
51
        }
52
        return $ret;
53
    }
54
55
    /**
56
     * Truncate the watch collection.
57
     *
58
     * @return void
59
     */
60
    public function truncate()
61
    {
62
        $this->storage->drop();
63
    }
64
65
}
66