Test Failed
Push — master ( e4a428...eca95d )
by Ricardo
04:31
created

Password::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 20
rs 9.9666
1
<?php
2
3
namespace Validate;
4
5
use Validate\Traits\BlockStringTrait;
6
use Validate\Traits\GetDataTrait;
7
8
/**
9
 * I created this validator just to avoid common passwords, you should not use to store passwords.
10
 * Not before you improve that shit!
11
 * 
12
 * Criei esse validator apenas para evitar senhas comuns, você não deveria usar para armazenar senhas.
13
 * Não antes de melhorar essa merda!
14
 */
15
class Password implements \Validate\Contracts\Validate
16
{
17
    use BlockStringTrait, GetDataTrait;
18
    
19
    /**
20
     * Create hash for store password
21
     *
22
     * @param string $password
23
     * @return string
24
     */
25
    public static function toDatabase(string $password)
26
    {
27
        return sha256($password);
0 ignored issues
show
Bug introduced by
The function sha256 was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        return /** @scrutinizer ignore-call */ sha256($password);
Loading history...
28
    }
29
30
    /**
31
     * Never use this function
32
     *
33
     * @param string $password
34
     * @return void
35
     */
36
    public static function toUser($password)
37
    {
38
        // @todo Nem deveria existir isso aqui !
39
        die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
40
        return null;
0 ignored issues
show
Unused Code introduced by
return null is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
41
    }
42
43
    /**
44
     * Verify if client use commoms passwords.
45
     * 
46
     * @todo Create validate for password force
47
     *
48
     * @param string $password
49
     * @param integer $force
50
     * @return void
51
     */
52
    public static function validate($password, $force = 0)
53
    {
54
        if (self::foundInMultiplesArrays([
0 ignored issues
show
Bug introduced by
array(array($password, s...('black-first-names'))) of type array<integer,array<integer,array|string>> is incompatible with the type string expected by parameter $blocks of Validate\Password::foundInMultiplesArrays(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        if (self::foundInMultiplesArrays(/** @scrutinizer ignore-type */ [
Loading history...
55
            [
56
                $password,
57
                self::getListFromFile('black-passwords')
58
            ],
59
            [
60
                $password,
61
                self::getListFromFile('black-names')
62
            ],
63
            [
64
                $password,
65
                self::getListFromFile('black-first-names')
66
            ],
67
        ])){
68
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
69
        }
70
71
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type void.
Loading history...
72
    }
73
74
    /**
75
     * Verify 
76
     *
77
     * @param string $fromDatabase
78
     * @param string $fromUser
79
     * @return boolean
80
     */
81
    public static function isSame(string $fromDatabase, string $fromUser)
82
    {
83
        return (self::toDatabase($fromDatabase)===self::toDatabase($fromUser));
84
85
    }
86
87
    public static function generate(
88
        $length = 8,
89
        $keyspace = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%'
90
    ) {
91
        $str = '';
92
        $max = mb_strlen($keyspace, '8bit') - 1;
93
        if ($max < 1) {
94
            throw new Exception('$keyspace must be at least two characters long');
0 ignored issues
show
Bug introduced by
The type Validate\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
95
        }
96
        for ($i = 0; $i < $length; ++$i) {
97
            $str .= $keyspace[random_int(0, $max)];
98
        }
99
        return $str;
100
    }
101
102
103
}
104