Completed
Push — master ( 9c906e...95818c )
by Ben
07:27
created

Field::isButton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Former\Traits;
3
4
use Former\Form\Form;
5
use Former\Form\Group;
6
use Former\Former;
7
use Former\Helpers;
8
use Former\Interfaces\FieldInterface;
9
use Former\LiveValidation;
10
use Illuminate\Container\Container;
11
use Illuminate\Support\Str;
12
13
/**
14
 * Abstracts general fields parameters (type, value, name) and
15
 * reforms a correct form field depending on what was asked
16
 */
17
abstract class Field extends FormerObject implements FieldInterface
18
{
19
	/**
20
	 * The IoC Container
21
	 *
22
	 * @var Container
23
	 */
24
	protected $app;
25
26
	/**
27
	 * The Form instance
28
	 *
29
	 * @var Former\Form
30
	 */
31
	protected $form;
32
33
	/**
34
	 * A label for the field (if not using Bootstrap)
35
	 *
36
	 * @var string
37
	 */
38
	protected $label;
39
40
	/**
41
	 * The field's group
42
	 *
43
	 * @var Group
44
	 */
45
	protected $group;
46
47
	/**
48
	 * The field's default element
49
	 *
50
	 * @var string
51
	 */
52
	protected $element = 'input';
53
54
	/**
55
	 * Whether the Field is self-closing or not
56
	 *
57
	 * @var boolean
58
	 */
59
	protected $isSelfClosing = true;
60
61
	/**
62
	 * The field's bind destination
63
	 *
64
	 * @var string
65
	 */
66
	protected $bind;
67
68
	/**
69
	 * Get the current framework instance
70
	 *
71
	 * @return Framework
72
	 */
73
	protected function currentFramework()
74
	{
75
		if ($this->app->bound('former.form.framework')) {
76
			return $this->app['former.form.framework'];
77
		}
78
79
		return $this->app['former.framework'];
80
	}
81
82
	////////////////////////////////////////////////////////////////////
83
	///////////////////////////// INTERFACE ////////////////////////////
84
	////////////////////////////////////////////////////////////////////
85
86
	/**
87
	 * Set up a Field instance
88
	 *
89
	 * @param string $type A field type
90
	 */
91
	public function __construct(Container $app, $type, $name, $label, $value, $attributes)
92
	{
93
		// Set base parameters
94
		$this->app   = $app;
95
		$this->type  = $type;
96
		$this->value = $value;
97
		$this->setAttributes($attributes);
98
		$this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null;
99
100
		// Compute and translate label
101
		$this->automaticLabels($name, $label);
102
103
		// Repopulate field
104
		if ($type != 'password' && $name !== '_token') {
105
			$this->value = $this->repopulate();
106
		}
107
108
		// Apply Live validation rules
109
		if ($this->app['former']->getOption('live_validation')) {
110
			$rules = new LiveValidation($this);
111
			$rules->apply($this->getRules());
112
		}
113
114
		// Bind the Group class
115
		$groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group';
116
		$groupClass = Former::FORMSPACE.$groupClass;
117
118
		$this->group = new $groupClass($this->app, $this->label);
119
	}
120
121
	/**
122
	 * Redirect calls to the group if necessary
123
	 *
124
	 * @param string $method
125
	 */
126
	public function __call($method, $parameters)
127
	{
128
		// Translate attributes
129
		$translatable = $this->app['former']->getOption('translatable', array());
130
		if (in_array($method, $translatable) and isset($parameters[0])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
131
			$parameters[0] = Helpers::translate($parameters[0]);
132
		}
133
134
		// Redirect calls to the Control Group
135
		if (method_exists($this->group, $method) or Str::startsWith($method, 'onGroup')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
136
			$method = str_replace('onGroup', '', $method);
137
			$method = lcfirst($method);
138
139
			call_user_func_array(array($this->group, $method), $parameters);
140
141
			return $this;
142
		}
143
144
		return parent::__call($method, $parameters);
145
	}
146
147
	/**
148
	 * Prints out the field, wrapped in its group
149
	 *
150
	 * @return string
151
	 */
152
	public function wrapAndRender()
153
	{
154
		// Dry syntax (hidden fields, plain fields)
155
		if ($this->isUnwrappable()) {
156
			$html = $this->render();
157
			// Control group syntax
158
		} elseif (Form::hasInstanceOpened()) {
159
			$html = $this->group->wrapField($this);
160
			// Classic syntax
161
		} else {
162
			$html = $this->currentFramework()->createLabelOf($this);
163
			$html .= $this->render();
164
		}
165
166
		return $html;
167
	}
168
169
	/**
170
	 * Prints out the field
171
	 *
172
	 * @return string
173
	 */
174
	public function __toString()
175
	{
176
		return $this->wrapAndRender();
177
	}
178
179
	////////////////////////////////////////////////////////////////////
180
	////////////////////////// PUBLIC INTERFACE ////////////////////////
181
	////////////////////////////////////////////////////////////////////
182
183
	/**
184
	 * Whether the current field is required or not
185
	 *
186
	 * @return boolean
187
	 */
188
	public function isRequired()
189
	{
190
		return isset($this->attributes['required']);
191
	}
192
193
	/**
194
	 * Set required live validation attribute
195
	 *
196
	 * @param boolean $isRequired
197
	 * @return $this
198
	 */
199
	public function required($isRequired=true)
200
	{
201
		if ($isRequired) {
202
			$this->attributes['required'] = true;
203
		} else {
204
			unset($this->attributes['required']);
205
		}
206
207
		return $this;
208
	}
209
210
	/**
211
	 * Check if a field is unwrappable (no label)
212
	 *
213
	 * @return boolean
214
	 */
215
	public function isUnwrappable()
216
	{
217
		return
218
			($this->form and $this->currentFramework()->is('Nude')) or
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
219
			($this->form and $this->isOfType('inline')) or
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
220
			$this->isButton() or
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
221
			$this->isOfType('hidden') or
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
222
			\Former\Form\Group::$opened or
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
223
			$this->group and $this->group->isRaw();
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
224
	}
225
226
	/**
227
	 * Check if field is a checkbox or a radio
228
	 *
229
	 * @return boolean
230
	 */
231
	public function isCheckable()
232
	{
233
		return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios');
234
	}
235
236
	/**
237
	 * Check if the field is a button
238
	 *
239
	 * @return boolean
240
	 */
241
	public function isButton()
242
	{
243
		return false;
244
	}
245
246
	/**
247
	 * Get the rules applied to the current field
248
	 *
249
	 * @return array An array of rules
250
	 */
251
	public function getRules()
252
	{
253
		return $this->app['former']->getRules($this->name);
254
	}
255
256
	////////////////////////////////////////////////////////////////////
257
	//////////////////////// SETTERS AND GETTERS ///////////////////////
258
	////////////////////////////////////////////////////////////////////
259
260
	/**
261
	 * Apply a Live Validation rule by chaining
262
	 *
263
	 * @param string $rule The rule
264
	 */
265
	public function rule($rule)
266
	{
267
		$parameters = func_get_args();
268
		array_shift($parameters);
269
270
		$live = new LiveValidation($this);
271
		$live->apply(array(
272
			$rule => $parameters,
273
		));
274
275
		return $this;
276
	}
277
278
    /**
279
     * Apply multiple rules passed as a string.
280
     *
281
     * @param $rules
282
     * @return $this
283
     */
284
    public function rules($rules)
285
    {
286
        if (!is_array($rules)) {
287
            $rules = explode('|', $rules);
288
        }
289
290
        foreach ($rules as $rule) {
291
            $parameters = null;
292
293
            // If we have a rule with a value
294
            if (($colon = strpos($rule, ':')) !== false) {
295
                $rulename = substr($rule, 0, $colon) ;
296
297
                /**
298
                * Regular expressions may contain commas and should not be divided by str_getcsv.
299
                * For regular expressions we are just using the complete expression as a parameter.
300
                */
301
                if ($rulename !== 'regex') {
302
                    $parameters = str_getcsv(substr($rule, $colon + 1));
303
                } else {
304
                    $parameters = [substr($rule, $colon + 1)];
305
                }
306
            }
307
308
            // Exclude unsupported rules
309
            $rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule;
310
311
            // Store processed rule in Former's array
312
            if (!isset($parameters)) {
313
                $parameters = array();
314
            }
315
316
            call_user_func_array([$this, 'rule'], array_merge([$rule], $parameters));
317
        }
318
319
        return $this;
320
    }
321
322
	/**
323
	 * Adds a label to the group/field
324
	 *
325
	 * @param  string $text       A label
326
	 * @param  array  $attributes The label's attributes
327
	 *
328
	 * @return Field              A field
329
	 */
330
	public function label($text, $attributes = array())
331
	{
332
		// Create the Label element
333
		$for   = $this->id ?: $this->name;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Former\Traits\Field>. 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...
334
		$label = $this->app['former']->label($text, $for, $attributes);
335
336
		// Set label
337
		$this->label = $label;
338
		if ($this->group) {
339
			$this->group->setLabel($label);
340
		}
341
342
		return $this;
343
	}
344
345
	/**
346
	 * Set the Field value no matter what
347
	 *
348
	 * @param string $value A new value
349
	 */
350
	public function forceValue($value)
351
	{
352
		$this->value = $value;
353
354
		return $this;
355
	}
356
357
	/**
358
	 * Classic setting of attribute, won't overwrite any populate() attempt
359
	 *
360
	 * @param  string $value A new value
361
	 */
362
	public function value($value)
363
	{
364
		// Check if we already have a value stored for this field or in POST data
365
		$already = $this->repopulate();
366
367
		if (!$already) {
368
			$this->value = $value;
369
		}
370
371
		return $this;
372
	}
373
374
	/**
375
	 * Change the field's name
376
	 *
377
	 * @param  string $name The new name
378
	 */
379
	public function name($name)
380
	{
381
		$this->name = $name;
382
383
		// Also relink the label to the new name
384
		$this->label($name);
385
386
		return $this;
387
	}
388
389
	/**
390
	 * Get the field's labels
391
	 *
392
	 * @return string
393
	 */
394
	public function getLabel()
395
	{
396
		return $this->label;
397
	}
398
399
	/**
400
	 * Change the field's bind destination
401
	 *
402
	 * @param $destination
403
	 */
404
	public function bind($destination) {
405
		$this->bind = $destination;
406
		if ($this->type != 'password') {
407
			$this->value = $this->repopulate();
408
		}
409
410
		return $this;
411
	}
412
413
	////////////////////////////////////////////////////////////////////
414
	//////////////////////////////// HELPERS ///////////////////////////
415
	////////////////////////////////////////////////////////////////////
416
417
	/**
418
	 * Use values stored in Former to populate the current field
419
	 */
420
	private function repopulate($fallback = null)
421
	{
422
		// Get values from POST, populated, and manually set value
423
		$post      = $this->app['former']->getPost($this->name);
424
		$populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator'];
425
		$populate  = $populator->get($this->bind ?: $this->name);
426
427
		// Assign a priority to each
428
		if (!is_null($post)) {
429
			return $post;
430
		}
431
		if (!is_null($populate)) {
432
			return $populate;
433
		}
434
435
		return $fallback ?: $this->value;
436
	}
437
438
	/**
439
	 * Ponders a label and a field name, and tries to get the best out of it
440
	 *
441
	 * @param  string $label A label
442
	 * @param  string $name  A field name
443
	 *
444
	 * @return false|null         A label and a field name
445
	 */
446
	private function automaticLabels($name, $label)
447
	{
448
		// Disabled automatic labels
449
		if (!$this->app['former']->getOption('automatic_label')) {
450
			$this->name = $name;
451
			$this->label($label);
452
453
			return false;
454
		}
455
456
		// Check for the two possibilities
457
		if ($label and is_null($name)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
458
			$name = Str::slug($label);
459
		} elseif (is_null($label) and $name) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
460
			$label = preg_replace('/\[\]$/', '', $name);
461
		}
462
463
		// Save values
464
		$this->name = $name;
465
		$this->label($label);
466
	}
467
}
468