Passed
Pull Request — master (#1477)
by Michael
48:41
created

Smarty4TemplateChecks::inspectFile()   C

Complexity

Conditions 14
Paths 36

Size

Total Lines 63
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 43
nc 36
nop 1
dl 0
loc 63
rs 6.2666
c 0
b 0
f 0

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 Smarty4TemplateChecks
18
 *
19
 * Scanner process to look for BC issues in existing templates when used with Smarty4
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 Smarty4TemplateChecks extends ScannerProcess
29
{
30
    protected $patterns = [
31
        'varname' => '/<{foreach[[:space:]]+item=([a-zA-Z0-9\-_.]+)[[:space:]]from=\$([a-zA-Z0-9\-_.]+) *}>/',
32
//        'noquotes' =>'/(<{xo[a-zA-Z\d]*\b[^}>]*?)\s*([^\'"}]+)(}>)/',
33
        // Updated noquotes pattern to ignore cases with variables or assignments
34
        'noquotes' => '/(<{xo[a-zA-Z\d]*\b)(?=[^}>]*[^\'"\$}>=]+[}>])([^}>]*?)(\s+)([^\$\'\"=]+)(\s*}>)/',
35
        'includeq' => '/(<{includeq[[:space:]]+[ -=\.\/_\'\"\$a-zA-Z0-9]+}>)/',
36
        'foreachq' => '/(<{foreachq[[:space:]]+[ -=\.\/_\'\"\$a-zA-Z0-9]+}>)/',
37
    ];
38
39
    /**
40
     * @var ScannerOutput
41
     */
42
    private $output;
43
44
    /**
45
     * @param ScannerOutput $output
46
     */
47
    public function __construct(ScannerOutput $output)
48
    {
49
        $this->output = $output;
50
    }
51
52
    /**
53
     * @param SplFileInfo $fileInfo
54
     * @return void
55
     */
56
    public function inspectFile(SplFileInfo $fileInfo)
57
    {
58
        $output = $this->output;
59
        $writable = $fileInfo->isWritable();
60
        $length = $fileInfo->getSize();
61
        $file = $fileInfo->openFile();
62
        $contents = $file->fread($length);
63
64
        // variable names in Smarty 3 foreach item and from must be unique
65
        $rule = 'varname';
66
        $pattern = $this->patterns[$rule];
67
        $results = preg_match_all($pattern, $contents, $matches, PREG_PATTERN_ORDER, 0);
68
        if ((0 < (int)$results) && isset($matches[0][0]) && is_string($matches[0][0])) {
69
            for ($i = 0; $i < (int)$results; $i++) {
70
                if ($matches[1][$i] == $matches[2][$i]) {
71
                    $file = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname());
72
                    $match = $matches[0][$i];
73
                    $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

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