Passed
Push — master ( cd8f20...b94509 )
by Michael
49:03
created

Smarty4TemplateRepair::inspectFile()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 7
nop 1
dl 0
loc 35
rs 9.2568
c 0
b 0
f 0
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 Smarty4TemplateRepair
18
 *
19
 * Scanner process to look for and repair 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 Smarty4TemplateRepair extends ScannerProcess
29
{
30
    /**
31
     * @var int count of changes made
32
     */
33
    protected $count = 0;
34
35
    /**
36
     * @var array regex patterns
37
     */
38
    protected $patterns = [];
39
40
    /**
41
     * @var array replacement patterns
42
     */
43
    protected $replacements = [];
44
45
    /**
46
     * @var ScannerOutput
47
     */
48
    private $output;
49
50
    /**
51
     * @param ScannerOutput $output
52
     */
53
    public function __construct(ScannerOutput $output)
54
    {
55
        $this->output = $output;
56
        $this->loadPatterns();
57
    }
58
59
    protected function loadPatterns()
60
    {
61
        $this->patterns[] = '/(<{includeq[[:space:]]+)/';
62
        $this->replacements[] = '<{include ';
63
64
        $this->patterns[] = '/(<{foreachq[[:space:]]+)/';
65
        $this->replacements[] = '<{foreach ';
66
67
        // For double quotes
68
        $this->patterns[] = '/("<{xo[a-zA-Z\d]*\b[^}>]*?)\s*([^\'"}=]+(?:=[^\'"}=]*)*)\s?}>/';
69
        $this->replacements[] = "$1 '$2'}>";
70
71
        // For single quotes
72
        $this->patterns[] = "/(\'<{xo[a-zA-Z\d]*\b[^}>]*?)\s*([^\'\"=]+(?:=[^\'\"=]*)*)\s?}>/";
73
        $this->replacements[] = '$1 "$2"}>';
74
75
        // For no surrounding quotes
76
        $this->patterns[] = '/(<{xo[a-zA-Z\d]*\b[^}>]*?)\s+([^\s\'\"=]+)(\s*}>)/';
77
        $this->replacements[] = '$1 \'$2\'$3';
78
    }
79
80
    /**
81
     * @param SplFileInfo $fileInfo
82
     * @return void
83
     */
84
    public function inspectFile(SplFileInfo $fileInfo)
85
    {
86
        $output = $this->output;
87
        if (false === $fileInfo->isWritable()) {
88
            return;
89
        }
90
91
        /** get and sanitize $filename used for error messages */
92
        $filename = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname());
93
94
        $length = $fileInfo->getSize();
95
        $file = $fileInfo->openFile('r+');
96
        $lines = $file->fread($length);
97
98
        $count = 0;
99
        $updatedLines = preg_replace(
100
            $this->patterns,
101
            $this->replacements,
102
            $lines,
103
            -1,
104
            $count,
105
        );
106
        if ($updatedLines === null) {
107
            trigger_error(sprintf('NULL return processing: %s', $filename), E_WARNING);
108
        }
109
110
        /* rewrite if changes were made */
111
        if ($count !== 0) {
112
            $file->fseek(0);
113
            $file->ftruncate(0);
114
            $result = $file->fwrite($updatedLines);
115
            if ($result == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $result of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
116
                trigger_error(sprintf('Error writing file: %s', $filename), E_WARNING);
117
            }
118
            $output->outputIssue($output->makeOutputIssue($filename, $count));
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

118
            $output->outputIssue($output->/** @scrutinizer ignore-call */ makeOutputIssue($filename, $count));
Loading history...
119
        }
120
    }
121
}
122