Completed
Push — master ( fc618d...debdac )
by Patrick
02:49
created

SQLPendingUser::__isset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Auth;
3
4
class SQLPendingUser extends PendingUser
5
{
6
    private $hash;
7
    private $time;
8
    private $blob;
9
    private $table;
10
11
    public function __construct($data, $table = false)
12
    {
13
        $this->hash = $data['hash'];
14
        $this->time = new \DateTime($data['time']);
15
        $this->blob = json_decode($data['data']);
16
        $this->table = $table;
17
    }
18
19
    public function __get($propName)
20
    {
21
        if(is_array($this->blob->{$propName}))
22
        {
23
            return $this->blob->{$propName}[0];
24
        }
25
        return $this->blob->{$propName};
26
    }
27
28
    public function __set($propName, $value)
29
    {
30
    }
31
32
    public function __isset($propName)
33
    {
34
       return isset($this->block->{$propName});
0 ignored issues
show
Documentation introduced by
The property block does not exist on object<Auth\SQLPendingUser>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
35
    }
36
37
    public function getHash()
38
    {
39
        return $this->hash;
40
    }
41
42
    public function getRegistrationTime()
43
    {
44
        return $this->time;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->time; (DateTime) is incompatible with the return type of the parent method Auth\PendingUser::getRegistrationTime of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
45
    }
46
47
    public function getPassword()
48
    {
49
        if(is_array($this->blob->password))
50
        {
51
            return $this->blob->password[0];
52
        }
53
        return $this->blob->password;
54
    }
55
56
    public function offsetGet($offset)
57
    {
58
        return $this->blob->$offset;
59
    }
60
61
    public function delete()
62
    {
63
        $this->table->delete(new \Data\Filter("hash eq '{$this->hash}'"));
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $this->table (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
64
    }
65
}
66
67