Completed
Push — master ( b38e7e...9a907a )
by Sophie
02:32
created

CheckPattern::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
1
<?php
2
3
/*
4
 * Copyright (C) 2016 Michael Herold <[email protected]>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace hemio\form;
21
22
/**
23
 * Check against a HTML5 form pattern
24
 *
25
 * @author Michael Herold <[email protected]>
26
 */
27
class CheckPattern extends Check {
28
29
    protected $pattern;
30
31
    /**
32
     * 
33
     * @param type $id 
34
     * @param type $pattern Regex pattern without anchors (example: [a-z]+)
35
     * @param type $message
36
     */
37
    public function __construct($id, $pattern, $message = null) {
38
        $this->pattern = '/^' . $pattern . '$/';
39
        $this->check = $this;
0 ignored issues
show
Bug introduced by
The property check 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...
40
        $this->id = $id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $id of type object<hemio\form\type> is incompatible with the declared type string of property $id.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
        $this->message = $message;
0 ignored issues
show
Documentation Bug introduced by
It seems like $message can also be of type object<hemio\form\type>. However, the property $message is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
42
43
        if (!is_int(preg_match($this->pattern, '')))
44
            throw new \Exception('Invalid pattern ' . $pattern . ' for check ' . $id);
45
    }
46
47
    public function __invoke($value) {
48
        return preg_match($pattern, $subject);
0 ignored issues
show
Bug introduced by
The variable $pattern does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $subject does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
49
    }
50
51
}
52