TimberUser::path()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This is used in Timber to represent users retrived from WordPress. You can call `$my_user = new TimberUser(123);` directly, or access it through the `{{ post.author }}` method.
5
 * @example
6
 * ```php
7
 * $context['current_user'] = new TimberUser();
8
 * $context['post'] = new TimberPost();
9
 * Timber::render('single.twig', $context);
10
 * ```
11
 * ```twig
12
 * <p class="current-user-info">Your name is {{ current_user.name }}</p>
13
 * <p class="article-info">This article is called "{{ post.title }}" and it's by {{ post.author.name }}
14
 * ```
15
 * ```html
16
 * <p class="current-user-info">Your name is Jesse Eisenberg</p>
17
 * <p class="article-info">This article is called "Consider the Lobster" and it's by David Foster Wallace
18
 * ```
19
 */
20
class TimberUser extends TimberCore implements TimberCoreInterface {
21
22
	public $object_type = 'user';
23
	public static $representation = 'user';
24
25
	public $_link;
26
27
	/**
28
	 * @api
29
	 * @var string The description from WordPress
30
	 */
31
	public $description;
32
	public $display_name;
33
34
	/**
35
	 * @api
36
	 * @var  string The first name of the user
37
	 */
38
	public $first_name;
39
40
	/**
41
	 * @api
42
	 * @var  string The last name of the user
43
	 */
44
	public $last_name;
45
46
	/**
47
	 * @api
48
	 * @var int The ID from WordPress
49
	 */
50
	public $id;
51
	public $user_nicename;
52
53
	/**
54
	 * @param int|bool $uid
55
	 */
56
	function __construct($uid = false) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
57
		$this->init($uid);
58
	}
59
60
	/**
61
	 * @example
62
	 * ```twig
63
	 * This post is by {{ post.author }}
64
	 * ```
65
	 * ```html
66
	 * This post is by Jared Novack
67
	 * ```
68
	 *
69
	 * @return string a fallback for TimberUser::name()
70
	 */
71
	function __toString() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
72
		$name = $this->name();
73
		if ( strlen($name) ) {
74
			return $name;
75
		}
76
		if ( strlen($this->name) ) {
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<TimberUser>. 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...
77
			return $this->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<TimberUser>. 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...
78
		}
79
		return '';
80
	}
81
82
	/**
83
	 * @internal
84
	 * @param string $field_name
85
	 * @return null
86
	 */
87
	function get_meta($field_name) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
88
		return $this->get_meta_field($field_name);
89
	}
90
91
	/**
92
	 * @internal
93
	 * @param string 	$field
94
	 * @param mixed 	$value
95
	 */
96
	function __set($field, $value) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
97
		if ( $field == 'name' ) {
98
			$this->display_name = $value;
99
		}
100
		$this->$field = $value;
101
	}
102
103
	/**
104
	 * @internal
105
	 * @param int|bool $uid The user ID to use
106
	 */
107
	protected function init($uid = false) {
108
		if ( $uid === false ) {
109
			$uid = get_current_user_id();
110
		}
111
		if ( is_object($uid) || is_array($uid) ) {
112
			$data = $uid;
113
			if ( is_array($uid) ) {
114
				$data = (object) $uid;
115
			}
116
			$uid = $data->ID;
117
		}
118
		if ( is_numeric($uid) ) {
119
			$data = get_userdata($uid);
120
		} else if ( is_string($uid) ) {
121
			$data = get_user_by('login', $uid);
122
		}
123
		if ( isset($data) && is_object($data) ) {
124
			if ( isset($data->data) ) {
125
				$this->import($data->data);
126
			} else {
127
				$this->import($data);
128
			}
129
		}
130
		$this->id = $this->ID;
131
		$this->name = $this->name();
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<TimberUser>. Since you implemented __set, maybe consider adding a @property annotation.

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...
132
		$custom = $this->get_custom();
133
		$this->import($custom);
0 ignored issues
show
Bug introduced by
It seems like $custom defined by $this->get_custom() on line 132 can also be of type null; however, TimberCore::import() does only seem to accept array|object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
134
	}
135
136
	/**
137
	 * @param string $field_name
138
	 * @return mixed
139
	 */
140 View Code Duplication
	function get_meta_field($field_name) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
		$value = null;
142
		$value = apply_filters('timber_user_get_meta_field_pre', $value, $this->ID, $field_name, $this);
143
		if ( $value === null ) {
144
			$value = get_user_meta($this->ID, $field_name, true);
145
		}
146
		$value = apply_filters('timber_user_get_meta_field', $value, $this->ID, $field_name, $this);
147
		return $value;
148
	}
149
150
	/**
151
	 * @return array|null
152
	 */
153
	function get_custom() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
154
		if ( $this->ID ) {
155
			$um = array();
156
			$um = apply_filters('timber_user_get_meta_pre', $um, $this->ID, $this);
157
			if ( empty($um) ) {
158
				$um = get_user_meta($this->ID);
159
			}
160
			$custom = array();
161 View Code Duplication
			foreach ($um as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
				if ( is_array($value) && count($value) == 1 ) {
163
					$value = $value[0];
164
				}
165
				$custom[$key] = maybe_unserialize($value);
166
			}
167
			$custom = apply_filters('timber_user_get_meta', $custom, $this->ID, $this);
168
			return $custom;
169
		}
170
		return null;
171
	}
172
173
	/**
174
	 * @api
175
	 * @return string http://example.org/author/lincoln
176
	 */
177
	public function link() {
178
		if ( !$this->_link ) {
179
			$this->_link = untrailingslashit(get_author_posts_url($this->ID));
180
		}
181
		return $this->_link;
182
	}
183
184
	/**
185
	 * @api
186
	 * @return string the human-friendly name of the user (ex: "Buster Bluth")
187
	 */
188
	function name() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
189
		return $this->display_name;
190
	}
191
192
	/**
193
	 * @param string $field_name
194
	 * @return mixed
195
	 */
196
	function meta($field_name) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
197
		return $this->get_meta_field($field_name);
198
	}
199
200
	/**
201
	 * @api
202
	 * @return string ex: /author/lincoln
203
	 */
204
	public function path() {
205
		return TimberURLHelper::get_rel_url($this->get_link());
0 ignored issues
show
Deprecated Code introduced by
The method TimberUser::get_link() has been deprecated with message: 0.21.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
206
	}
207
208
	/**
209
	 * @api
210
	 * @return string ex baberaham-lincoln
211
	 */
212
	public function slug() {
213
		return $this->user_nicename;
214
	}
215
216
	/**
217
	 * @deprecated 0.21.9
218
	 * @return string The link to a user's profile page
219
	 */
220
	function get_link() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
221
		return $this->link();
222
	}
223
224
	/**
225
	 * @deprecated 0.21.8
226
	 * @return string ex: /author/lincoln
227
	 */
228
	function get_path() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
229
		return $this->path();
230
	}
231
232
	/**
233
	 * @deprecated 0.21.8
234
	 * @return string
235
	 */
236
	function get_permalink() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
237
		return $this->get_link();
0 ignored issues
show
Deprecated Code introduced by
The method TimberUser::get_link() has been deprecated with message: 0.21.9

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
238
	}
239
240
	/**
241
	 * @deprecated 0.21.8
242
	 * @return string
243
	 */
244
	function permalink() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
245
		return $this->get_permalink();
0 ignored issues
show
Deprecated Code introduced by
The method TimberUser::get_permalink() has been deprecated with message: 0.21.8

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
246
	}
247
248
}
249