Smarty3TemplateChecks   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 51
c 2
b 1
f 0
dl 0
loc 89
rs 10
wmc 18

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C inspectFile() 0 63 17
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' =>'/(<{xo[a-zA-Z\d]*\b[^}>]*?)\s*([^\'"}]+)(}>)/',
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 ((0 < (int)$results) && isset($matches[0][0]) && is_string($matches[0][0])) {
67
            for ($i = 0; $i < (int)$results; $i++) {
68
                if ($matches[1][$i] == $matches[2][$i]) {
69
                    $file = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname());
70
                    $match = $matches[0][$i];
71
                    $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

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