Completed
Push — master ( 416cb1...222de5 )
by Anton
03:44
created

User::__invoke()   C

Complexity

Conditions 16
Paths 44

Size

Total Lines 73
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 73
rs 5.4157
c 1
b 0
f 0
cc 16
eloc 32
nc 44
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Modules\Entitizer\Controller {
4
5
	use Modules\Auth, Modules\Entitizer, Utils\Validate, Str;
6
7
	class User {
8
9
		private $user = null;
10
11
		# Constructor
12
13
		public function __construct(Entitizer\Entity\User $user) {
14
15
			$this->user = $user;
16
		}
17
18
		# Invoker
19
20
		public function __invoke(array $post) {
21
22
			# Declare variables
23
24
			$name = ''; $email = ''; $rank = ''; $first_name = ''; $last_name = ''; $sex = '';
25
26
			$city = ''; $country = ''; $timezone = ''; $password = ''; $password_retype = '';
27
28
			# Extract post array
29
30
			extract($post);
31
32
			# Validate name & email
33
34
			if (false === ($name = Validate::userName($name))) return 'USER_ERROR_NAME_INVALID';
35
36
			if (false === ($email = Validate::userEmail($email))) return 'USER_ERROR_EMAIL_INVALID';
37
38
			# Validate password
39
40
			if ((0 === $this->user->id) || ('' !== $password)) {
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in Modules\Entitizer\Utils\Entity. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
41
42
				if (false === ($password = Validate::userPassword($password))) return 'USER_ERROR_PASSWORD_INVALID';
43
44
				if (0 !== strcmp($password, $password_retype)) return 'USER_ERROR_PASSWORD_MISMATCH';
45
			}
46
47
			# Check name exists
48
49
			if (false === ($check_name = $this->user->check('name', $name))) return 'USER_ERROR_MODIFY';
50
51
			if ($check_name === 1) return 'USER_ERROR_NAME_DUPLICATE';
52
53
			# Check email exists
54
55
			if (false === ($check_email = $this->user->check('email', $email))) return 'USER_ERROR_MODIFY';
56
57
			if ($check_email === 1) return 'USER_ERROR_EMAIL_DUPLICATE';
58
59
			# Modify user
60
61
			$data = [];
62
63
			$data['name']               = $name;
64
			$data['email']              = $email;
65
			$data['rank']               = $rank;
66
			$data['first_name']         = $first_name;
67
			$data['last_name']          = $last_name;
68
			$data['sex']                = $sex;
69
			$data['city']               = $city;
70
			$data['country']            = $country;
71
			$data['timezone']           = $timezone;
72
73
			if ((0 === $this->user->id) || ('' !== $password)) {
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in Modules\Entitizer\Utils\Entity. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
74
75
				$data['auth_key']           = ($auth_key = Str::random(40));
76
				$data['password']           = Str::encode($auth_key, $password);
77
			}
78
79
			if (0 === $this->user->id) {
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in Modules\Entitizer\Utils\Entity. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
80
81
				$data['time_registered']    = REQUEST_TIME;
82
				$data['time_logged']        = REQUEST_TIME;
83
			}
84
85
			$modifier = ((0 === $this->user->id) ? 'create' : 'edit');
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in Modules\Entitizer\Utils\Entity. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
86
87
			if (!$this->user->$modifier($data)) return 'USER_ERROR_MODIFY';
88
89
			# ------------------------
90
91
			return true;
92
		}
93
	}
94
}
95