MACValidator::validateValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 5
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 5
loc 5
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @author: onixsib <[email protected]>
4
 * @date:   03.08.2016
5
 * @copyright onixsib
6
 * @link http://onixsib.ru/
7
 * @file MACValidator
8
 */
9
namespace onixsib\validators;
10
11
use Yii;
12
use yii\validators\Validator;
13
14
/**
15
 * Class MACValidator
16
 */
17 View Code Duplication
class MACValidator extends Validator {
0 ignored issues
show
Duplication introduced by
This class 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...
18
	/**
19
	 * @inheritdoc
20
	 */
21
	public function init() {
22
		parent::init();
23
		if ($this->message === null) {
24
			$this->message = \Yii::t('yii', "{attribute} is invalid.");
25
		}
26
	}
27
28
	public function validateAttribute($model, $attribute) {
29
		$string = $model->{$attribute};
30
		if (!$this->validateValue($string)) {
31
			$this->addError($model, $attribute, \Yii::t('yii', 'This in not valid MAC address {sample}', ['sample' => '00:00:00:00:00:01']));
32
			return false;
33
		}
34
		return true;
35
	}
36
37
	public function validateValue($string) {
38
39
		// filter_var suxx
40
		return preg_match('/^([0-9a-F]{1,2}[\.:-]){5}([0-9a-F]{1,2})$/', $string);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return preg_match('/^([0...-F]{1,2})$/', $string); (integer) is incompatible with the return type of the parent method yii\validators\Validator::validateValue of type array|null.

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...
41
	}
42
}
43
44
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...