Completed
Push — master ( 5bff48...0eeb04 )
by Oscar
02:44
created

ExtendedSelectionTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 92
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A from() 0 6 1
A field() 0 6 1
B relatedWith() 0 29 4
A fieldsToString() 0 4 2
A fromToString() 0 4 2
1
<?php
2
3
namespace SimpleCrud\Queries;
4
5
use SimpleCrud\RowInterface;
6
7
/**
8
 * Extended trait
9
 *
10
 * @property \SimpleCrud\Entity $entity
11
 */
12
trait ExtendedSelectionTrait
13
{
14
    use SelectionTrait;
15
16
    protected $from = [];
17
    protected $fields = [];
18
19
    /**
20
     * Adds new extra table to the query.
21
     *
22
     * @param string $table
23
     *
24
     * @return self
25
     */
26
    public function from($table)
27
    {
28
        $this->from[] = $table;
29
30
        return $this;
31
    }
32
33
    /**
34
     * Adds a new extra field to the query.
35
     *
36
     * @param string $field
37
     *
38
     * @return self
39
     */
40
    public function field($field)
41
    {
42
        $this->fields[] = $field;
43
44
        return $this;
45
    }
46
47
    /**
48
     * Adds a WHERE according with the relation of other entity.
49
     *
50
     * @param RowInterface $row
51
     *
52
     * @return self
53
     */
54
    public function relatedWith(RowInterface $row)
55
    {
56
        $entity = $row->getEntity();
57
58
        if ($this->entity->hasOne($entity)) {
59
            return $this->by($entity->foreignKey, $row->get('id'));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SimpleCrud\RowInterface as the method get() does only exist in the following implementations of said interface: SimpleCrud\Row, SimpleCrud\RowCollection.

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...
60
        }
61
62
        if ($this->entity->hasMany($entity)) {
63
            return $this->byId($row->get($this->entity->foreignKey));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SimpleCrud\RowInterface as the method get() does only exist in the following implementations of said interface: SimpleCrud\Row, SimpleCrud\RowCollection.

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...
64
        }
65
66
        $bridge = $this->entity->getBridge($entity);
67
68
        if ($bridge) {
69
            $this->from($bridge->name);
70
            $this->from($entity->name);
71
72
            $this->fields[] = "`{$bridge->name}`.`{$entity->foreignKey}`";
73
74
            $this->where("`{$bridge->name}`.`{$this->entity->foreignKey}` = `{$this->entity->name}`.`id`");
75
            $this->where("`{$bridge->name}`.`{$entity->foreignKey}` = `{$entity->name}`.`id`");
76
            $this->where("`{$entity->name}`.`id` IN (:{$bridge->name})", [":{$bridge->name}" => $row->get('id')]);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SimpleCrud\RowInterface as the method get() does only exist in the following implementations of said interface: SimpleCrud\Row, SimpleCrud\RowCollection.

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...
77
78
            return $this;
79
        }
80
81
        throw new SimpleCrudException("The tables {$this->entity->name} and {$entity->name} are no related");
82
    }
83
84
    /**
85
     * add extra fields to the code.
86
     *
87
     * @return string
88
     */
89
    protected function fieldsToString($prepend = ', ')
90
    {
91
        return $this->fields ? $prepend.implode(', ', $this->fields) : '';
92
    }
93
94
    /**
95
     * add extra fields to the code.
96
     *
97
     * @return string
98
     */
99
    protected function fromToString($prepend = ', ')
100
    {
101
        return $this->from ? $prepend.'`'.implode('`, `', $this->from).'`' : '';
102
    }
103
}
104