Passed
Push — 0.3.0 ( 0d73f1...9a02d7 )
by Anton
04:10
created

Collection::castOrderBy()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 6
nc 8
nop 1
1
<?php
2
3
namespace Modules\Entitizer\Utils {
4
5
	use Modules\Entitizer, Config;
6
7
	abstract class Collection {
8
9
		protected $definition = null, $config = null;
10
11
		# Cast order set to be suitable with entity definition
12
13
		private function castOrderBy(array $data) {
14
15
			$order_by = [];
16
17
			foreach ($data as $field => $direction) {
18
19
				if (false === ($param = $this->definition->param($field))) continue;
20
21
				$order_by[$param->name] = ((strtoupper($direction) !== 'DESC') ? 'ASC' : 'DESC');
22
			}
23
24
			# ------------------------
25
26
			return (([] !== $order_by) ? $order_by : static::$order_by);
27
		}
28
29
		# Get selection
30
31
		protected function getSelection() {
32
33
			$selection = [];
34
35
			foreach ($this->definition->paramsSecure() as $field) $selection[] = ('ent.' . $field);
36
37
			# ------------------------
38
39
			return implode(', ', $selection);
40
		}
41
42
		# Get order by
43
44
		protected function getOrderBy(array $data) {
45
46
			$order_by = [];
47
48
			foreach ($this->castOrderBy($data) as $field => $direction) $order_by[] = ('ent.' . $field . ' ' . $direction);
49
50
			# ------------------------
51
52
			return implode(', ', $order_by);
53
		}
54
55
		# Get condition
56
57
		protected function getCondition(array $data) {
58
59
			return implode(' AND ', array_filter($this->config->cast($data, true)));
60
		}
61
62
		# Constructor
63
64
		public function __construct() {
65
66
			$this->definition = Entitizer::definition(static::$table);
67
68
			$this->config = new Config();
69
70
			$this->init();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Modules\Entitizer\Utils\Collection as the method init() does only exist in the following sub-classes of Modules\Entitizer\Utils\Collection: Modules\Entitizer\Listview\Menuitems, Modules\Entitizer\Listview\Pages, Modules\Entitizer\Listview\Users, Modules\Entitizer\Listview\Variables, Modules\Entitizer\Listview\Widgets, Modules\Entitizer\Treeview\Menuitems, Modules\Entitizer\Treeview\Pages, Modules\Entitizer\Treeview\Users, Modules\Entitizer\Treeview\Variables, Modules\Entitizer\Treeview\Widgets. 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...
71
		}
72
	}
73
}
74