ThreemaGateway_Option_DebugModeLog   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 120
Duplicated Lines 5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 6
loc 120
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A renderOption() 3 22 3
A verifyOption() 0 17 2
A removeLog() 0 13 3
B correctOption() 3 24 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Debug mode log option.
4
 *
5
 * @package ThreemaGateway
6
 * @author rugk
7
 * @copyright Copyright (c) 2016 rugk
8
 * @license MIT
9
 */
10
11
class ThreemaGateway_Option_DebugModeLog
12
{
13
    /**
14
     * @var string Default file path
15
     */
16
    const DEFAULT_PATH = 'internal_data/threemagateway/receivedmsgs.log';
17
18
    /**
19
     * Renders the debug mode log setting.
20
     *
21
     * Basically it just hides the setting if the debug mode of XenForo is disabled.
22
     *
23
     * @param XenForo_View $view           View object
24
     * @param string       $fieldPrefix    Prefix for the HTML form field name
25
     * @param array        $preparedOption Prepared option info
26
     * @param bool         $canEdit        True if an "edit" link should appear
27
     *
28
     * @return XenForo_Template_Abstract Template object
29
     */
30
    public static function renderOption(XenForo_View $view, $fieldPrefix, array $preparedOption, $canEdit)
31
    {
32
        $preparedOption['option_value'] = self::correctOption($preparedOption['option_value']);
33
34
        // hide option when disabled and debug mode is off (so that users are not confused)
35 View Code Duplication
        if (!XenForo_Application::debugMode() && !$preparedOption['option_value']['enabled']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
            return XenForo_ViewAdmin_Helper_Option::renderOptionTemplateInternal('threemagateway_option_list_option_hidden', $view, $fieldPrefix, $preparedOption, $canEdit);
37
        }
38
39
        // set options
40
        $preparedOption['edit_format']  = 'onofftextbox';
41
        $preparedOption['formatParams'] = [
42
            'onoff' => 'enabled',
43
            'value' => 'path',
44
            'type' => 'textbox',
45
            'default' => self::DEFAULT_PATH,
46
            'placeholder' => self::DEFAULT_PATH
47
        ];
48
49
        //pass this to the default handler
50
        return XenForo_ViewAdmin_Helper_Option::renderPreparedOptionHtml($view, $preparedOption, $canEdit);
51
    }
52
53
    /**
54
     * Verifies whether the dir of the file is valid (can be created) and is writable.
55
     *
56
     * @param string             $filepath   Input
57
     * @param XenForo_DataWriter $dataWriter
58
     * @param string             $fieldName  Name of field/option
59
     *
60
     * @return bool
61
     */
62
    public static function verifyOption(&$filepath, XenForo_DataWriter $dataWriter, $fieldName)
63
    {
64
        $filepath = self::correctOption($filepath);
0 ignored issues
show
Documentation introduced by
$filepath is of type string, but the function expects a array.

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...
65
66
        // check path & (create) dir
67
        $dirpath     = dirname($filepath['path']);
68
        $absoluteDir = XenForo_Application::getInstance()->getRootDir() . '/' . $dirpath;
69
        if (!ThreemaGateway_Handler_Validation::checkDir($absoluteDir)) {
70
            $dataWriter->error(new XenForo_Phrase('threemagw_invalid_debuglogpath'), $fieldName);
71
            return false;
72
        }
73
74
        // auto-remove existing file if necessary
75
        self::removeLog($filepath);
76
77
        return true;
78
    }
79
80
    /**
81
     * Remove the log file.
82
     *
83
     * @param  array $option option setting
84
     * @return bool
85
     */
86
    public static function removeLog($option)
87
    {
88
        // to be sure check the path again
89
        $option = self::correctOption($option);
90
91
        // check pre-conditions
92
        if (!$option['enabled'] || !file_exists($option['path'])) {
93
            return false;
94
        }
95
96
        // remove file
97
        return unlink(realpath($option['path']));
98
    }
99
100
    /**
101
     * Corrects the option array.
102
     *
103
     * @param  array  $option
104
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
105
     */
106
    protected static function correctOption($option)
107
    {
108
        // correct value
109
        if (empty($option)) {
110
            /** @var XenForo_Options $xenOptions */
111
            $xenOptions = XenForo_Application::getOptions();
112
113
            // save file path even if disabled
114
            $option['enabled'] = 0;
115
            $option['path']    = $xenOptions->threema_gateway_logreceivedmsgs['path'];
116
        }
117
118
        // set default value
119
        if (empty($option['path'])) {
120
            $option['path'] = self::DEFAULT_PATH;
121
        }
122
123
        // correct path
124 View Code Duplication
        if (substr($option['path'], 0, 1) == '/') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
            $option['path'] = substr($option['path'], 1);
126
        }
127
128
        return $option;
129
    }
130
}
131