correctOption()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

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 4
nc 2
nop 1
1
<?php
2
/**
3
 * Private key path option.
4
 *
5
 * @package ThreemaGateway
6
 * @author rugk
7
 * @copyright Copyright (c) 2016 rugk
8
 * @license MIT
9
 */
10
11
class ThreemaGateway_Option_PrivateKeyPath
12
{
13
    /**
14
     * Verifies the existence of the path.
15
     *
16
     * Note that $filepath can also be the key directly. However this is neither
17
     * the official way nor recommend/documented.
18
     *
19
     * @param string             $filepath   Input
20
     * @param XenForo_DataWriter $dataWriter
21
     * @param string             $fieldName  Name of field/option
22
     *
23
     * @return bool
24
     */
25
    public static function verifyOption(&$filepath, XenForo_DataWriter $dataWriter, $fieldName)
26
    {
27
        $filepath = self::correctOption($filepath);
28
29
        // check path
30
        if ($filepath != '' && //ignore/allow empty field
31
            !self::isValidPath($filepath) && //verify path
32
            !ThreemaGateway_Helper_Key::check($filepath, 'private:') //allow private key string directly
33
        ) {
34
            $dataWriter->error(new XenForo_Phrase('threemagw_invalid_privkeypath'), $fieldName);
35
            return false;
36
        }
37
38
        return true;
39
    }
40
41
    /**
42
     * Remove the log file.
43
     *
44
     * @param  array $filepath option setting
45
     * @return bool
46
     */
47
    public static function removePrivateKey($filepath)
48
    {
49
        // to be sure check the path again
50
        $filepath = self::correctOption($filepath);
0 ignored issues
show
Documentation introduced by
$filepath is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
52
        // check pre-conditions
53
        if (!self::isValidPath($filepath)) {
54
            return false;
55
        }
56
57
        // remove file
58
        return unlink(realpath(__DIR__ . '/../' . $filepath));
59
    }
60
61
    /**
62
     * Corrects the option file path.
63
     *
64
     * @param  string $filepath
65
     * @return bool
66
     */
67
    protected static function isValidPath($filepath)
68
    {
69
        return (
70
            $filepath != '' && // empty strings can never be a valid path
71
            file_exists(__DIR__ . '/../' . $filepath) //verify accessibility
72
        );
73
    }
74
75
    /**
76
     * Corrects the option file path.
77
     *
78
     * @param  string $filepath
79
     * @return string
80
     */
81
    protected static function correctOption($filepath)
82
    {
83
        // correct path
84
        if (substr($filepath, 0, 1) == '/') {
85
            $filepath = substr($filepath, 1);
86
        }
87
88
        return $filepath;
89
    }
90
}
91