Completed
Push — master ( ba371c...d5d12c )
by Ronan
15s
created

Hetu::validate()   C

Complexity

Conditions 11
Paths 7

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 22
nc 7
nop 1

How to fix   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 IsoCodes;
4
5
/**
6
 * Class Hetu, Finnish personal identity code (HETU, Henkilötunnus).
7
 *
8
 * @link https://en.wikipedia.org/wiki/National_identification_number#Finland
9
 * @link http://vrk.fi/en/personal-identity-code1
10
 */
11
class Hetu implements IsoCodeInterface
12
{
13
    /**
14
     * @var array
15
     */
16
    public static $centuryCodes = [
17
        '+' => 1800,
18
        '-' => 1900,
19
        'A' => 2000,
20
    ];
21
22
    /**
23
     * @var string
24
     */
25
    public static $validationKeys = '0123456789ABCDEFHJKLMNPRSTUVWXY';
26
27
    /**
28
     * HETU validator.
29
     *
30
     * @param string $hetu
31
     *
32
     * @return bool
33
     */
34
    public static function validate($hetu)
35
    {
36
        if (!is_string($hetu) || strlen($hetu) != 11) {
37
            return false;
38
        }
39
40
        $dd = substr($hetu, 0, 2);
41
        $mm = substr($hetu, 2, 2);
42
        $yy = substr($hetu, 4, 2);
43
        $centuryCode = strtoupper($hetu{6});
44
        $id = (int) ($dd.$mm.$yy.substr($hetu, 7, 3));
45
        $checksum = strtoupper($hetu{10});
46
47
        if ($dd < 1 || $dd > 31) {
48
            return false;
49
        }
50
51
        if ($mm < 1 || $mm > 12) {
52
            return false;
53
        }
54
55
        if (!is_numeric($yy)) {
56
            return false;
57
        }
58
59
        if (!array_key_exists($centuryCode, static::$centuryCodes)) {
60
            return $centuryCode;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $centuryCode; (string) is incompatible with the return type declared by the interface IsoCodes\IsoCodeInterface::validate 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...
61
        }
62
63
        $hetuChecksumKey = $id % strlen(static::$validationKeys);
64
        if (!isset(static::$validationKeys{$hetuChecksumKey})
65
            || static::$validationKeys{$hetuChecksumKey} !== $checksum
66
        ) {
67
            return false;
68
        }
69
70
        return true;
71
    }
72
}
73