Passed
Pull Request — master (#1)
by
unknown
01:35
created

User::socials()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 20
rs 9.8666
cc 4
nc 4
nop 1
1
<?php
2
3
namespace Example\Models;
4
5
use CoffeeCode\DataLayer\DataLayer;
6
7
/**
8
 * Class User
9
 * @package Example\Models
10
 */
11
class User extends DataLayer
12
{
13
    /**
14
     * User constructor.
15
     */
16
    public function __construct()
17
    {
18
        parent::__construct("users", ["first_name", "last_name"]);
19
    }
20
21
    /**
22
     * @param bool $obj
23
     * @return User
24
     */
25
    public function address(bool $obj = false): User
26
    {
27
        $this->relational_fields[] = 'address';
28
29
        if ($obj) {
30
            $this->address = (new Address())->findByUser($this->id);
0 ignored issues
show
Bug introduced by
$this->id of type null|string is incompatible with the type integer expected by parameter $user_id of Example\Models\Address::findByUser(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
            $this->address = (new Address())->findByUser(/** @scrutinizer ignore-type */ $this->id);
Loading history...
Bug Best Practice introduced by
The property id does not exist on Example\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property address does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
            return $this;
32
        }
33
34
        $address = (new Address())->findByUser($this->id);
35
        if (empty($address)) {
36
            $this->address = null;
37
        } else {
38
            $this->address = (new Address())->findByUser($this->id)->data();
39
        }
40
        return $this;
41
    }
42
43
    /**
44
     * @param bool $obj
45
     * @return User
46
     */
47
    public function socials(bool $obj = false): User
48
    {
49
        $this->relational_fields[] = 'socials';
50
        $socials = (new Socials())->getUserSocials($this->id);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Example\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
$this->id of type null|string is incompatible with the type integer expected by parameter $user_id of Example\Models\Socials::getUserSocials(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $socials = (new Socials())->getUserSocials(/** @scrutinizer ignore-type */ $this->id);
Loading history...
51
52
        if ($obj) {
53
            $this->socials = $socials;
0 ignored issues
show
Bug Best Practice introduced by
The property socials does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
54
            return $this;
55
        }
56
57
        if (empty($socials)) {
58
            $this->socials = null;
59
            return $this;
60
        }
61
62
        foreach ($socials as $key => $value) {
63
            $user_socials[] = $value->data();
64
        }
65
        $this->socials = $user_socials;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user_socials seems to be defined by a foreach iteration on line 62. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
66
        return $this;
67
    }
68
}