Passed
Pull Request — master (#1375)
by Richard
08:11
created

Smarty3TemplateChecks::inspectFile()   B

Complexity

Conditions 11
Paths 81

Size

Total Lines 61
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 42
c 1
b 0
f 0
nc 81
nop 1
dl 0
loc 61
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xoops\Upgrade;
13
14
use SplFileInfo;
15
16
/**
17
 * XOOPS Upgrade Smarty3TemplateChecks
18
 *
19
 * Scanner process to look for BC issues in existing templates when used with Smarty3
20
 *
21
 * @category  Xoops\Upgrade
22
 * @package   Xoops
23
 * @author    Richard Griffith <[email protected]>
24
 * @copyright 2023 XOOPS Project (https://xoops.org)
25
 * @license   GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
26
 * @link      https://xoops.org
27
 */
28
class Smarty3TemplateChecks extends ScannerProcess
29
{
30
    protected $patterns = array(
31
        'varname' => '/<{foreach[[:space:]]+item=([a-zA-Z0-9\-_.]+)[[:space:]]from=\$([a-zA-Z0-9\-_.]+) *}>/',
32
        'noquotes' => '/(<{[a-zA-Z0-1_\-]+[[:space:]]+[a-zA-Z.]+}>)/',
33
        'includeq' => '/(<{includeq[[:space:]]+[ -=\.\/_\'\"\$a-zA-Z0-9]+}>)/',
34
        'foreachq' => '/(<{foreachq[[:space:]]+[ -=\.\/_\'\"\$a-zA-Z0-9]+}>)/',
35
    );
36
37
    /**
38
     * @var ScannerOutput
39
     */
40
    private $output;
41
42
    /**
43
     * @param ScannerOutput $output
44
     */
45
    public function __construct(ScannerOutput $output)
46
    {
47
        $this->output = $output;
48
    }
49
50
    /**
51
     * @param SplFileInfo $fileInfo
52
     * @return void
53
     */
54
    public function inspectFile(SplFileInfo $fileInfo)
55
    {
56
        $output = $this->output;
57
        $writable = $fileInfo->isWritable();
58
        $length = $fileInfo->getSize();
59
        $file = $fileInfo->openFile();
60
        $contents = $file->fread($length);
61
62
        // variable names in Smarty 3 foreach item and from must be unique
63
        $rule = 'varname';
64
        $pattern = $this->patterns[$rule];
65
        $results = preg_match_all($pattern, $contents, $matches, PREG_PATTERN_ORDER, 0);
66
        if ((false !== $results) && @is_string($matches[0][0])) {
67
            if ($matches[1][0] == $matches[2][0]) {
68
                $file = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname());
69
                $match = $matches[0][0];
70
                $output->outputIssue($output->makeOutputIssue($rule, $file, $match, $writable));
0 ignored issues
show
Bug introduced by
The method makeOutputIssue() does not exist on Xoops\Upgrade\ScannerOutput. Since it exists in all sub-types, consider adding an abstract or default implementation to Xoops\Upgrade\ScannerOutput. ( Ignorable by Annotation )

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

70
                $output->outputIssue($output->/** @scrutinizer ignore-call */ makeOutputIssue($rule, $file, $match, $writable));
Loading history...
71
            }
72
        }
73
        unset($matches);
74
75
        // plugin function arguments must be quoted
76
        $rule = 'noquotes';
77
        $pattern = $this->patterns[$rule];
78
        $results = preg_match_all($pattern, $contents, $matches, PREG_PATTERN_ORDER, 0);
79
        if (false !== $results) {
80
            @$match = $matches[0][0];
81
            if (!is_null($match)
82
                && '<{if false}>' != $match // oddball case
83
            ) {
84
                $file = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname());
85
                $output->outputIssue($output->makeOutputIssue($rule, $file, $match, $writable));
86
            }
87
        }
88
        unset($matches);
89
90
        // includeq was removed, use include instead
91
        $rule = 'includeq';
92
        $pattern = $this->patterns[$rule];
93
        $results = preg_match_all($pattern, $contents, $matches, PREG_PATTERN_ORDER, 0);
94
        if (false !== $results) {
95
            @$match = $matches[0][0];
96
            if (!is_null($match)) {
97
                $file = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname());
98
                $output->outputIssue($output->makeOutputIssue($rule, $file, $match, $writable));
99
            }
100
        }
101
        unset($matches);
102
103
        // foreachq was removed, use foreach instead
104
        $rule = 'foreachq';
105
        $pattern = $this->patterns[$rule];
106
        $results = preg_match_all($pattern, $contents, $matches, PREG_PATTERN_ORDER, 0);
107
        if (false !== $results) {
108
            @$match = $matches[0][0];
109
            if (!is_null($match)) {
110
                $file = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname());
111
                $output->outputIssue($output->makeOutputIssue($rule, $file, $match, $writable));
112
            }
113
        }
114
        unset($matches);
115
    }
116
}
117