Configuration::getPatternCharacters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace raphiz\passwordcards;
4
5
class Configuration
6
{
7
8
    const DEFAULT_PATTERN = 'a-zA-Z0-9~*-*';
9
10
    const LOWER = "abcdefghijklmnopqrstuvwxyz";
11
12
    const UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
13
14
    const NUMBERS = "0123456789";
15
16
    const SYMBOLS = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
17
18
    public $seed = null;
19
20
    public $pattern = null;
21
22
    public $primaryColor = null;
23
24
    public $secondaryColor = null;
25
26
    public $text = null;
27
28
    public $keys = null;
29
30
    public function __construct($seed, $pattern, $keys, $spaceBarSize, $text, $primaryColor, $secondaryColor)
31
    {
32
        $this->seed = self::evalSeed($seed);
33
        $this->pattern = self::evalPattern($pattern);
34
        $this->keys = self::evalKeys($keys);
35
        $this->primaryColor = $primaryColor;
36
        $this->text = $text;
37
        $this->spaceBarSize = $spaceBarSize;
0 ignored issues
show
Bug introduced by
The property spaceBarSize does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
        $this->secondaryColor = $secondaryColor;
39
    }
40
41
    public function getPatternCharacters()
42
    {
43
        return str_split(self::completePattern($this->pattern));
44
    }
45
46
    public static function completePattern($pattern)
47
    {
48
        $pattern = str_replace('a-z', self::LOWER, $pattern);
49
        $pattern = str_replace('A-Z', self::UPPER, $pattern);
50
        $pattern = str_replace('0-9', self::NUMBERS, $pattern);
51
        $pattern = str_replace('*-*', self::SYMBOLS, $pattern);
52
        return $pattern;
53
    }
54
55
    /**
56
     * If no (numeric) seed is provided, generate one.
57
     */
58
    public static function evalSeed($seed)
59
    {
60
        if ($seed === null || !is_numeric($seed)) {
61
            list($usec, $sec) = explode(' ', microtime());
62
            $seed = (float) $sec + ((float) $usec * 100000);
63
        }
64
        return $seed;
65
    }
66
67
    /**
68
    * If no pattern is provided, return the default.
69
    */
70
    public static function evalPattern($pattern)
71
    {
72
        if ($pattern === null) {
73
            $pattern = self::DEFAULT_PATTERN;
74
        }
75
        return $pattern;
76
    }
77
78
    /**
79
    * If no keys are provided, use qwerty.
80
    */
81
    public static function evalKeys($keys)
82
    {
83
        // Return qwertz
84
        if (strtolower($keys) === 'qwertz') {
85
            return 'QWERTZUIOPASDFGHJKLYXCVBNM';
86
        }
87
        // Return qwerty
88
        return 'QWERTYUIOPASDFGHJKLZXCVBNM';
89
    }
90
}
91