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

Smarty3TemplateRepair::inspectFile()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 22
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 32
rs 9.2568
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 Smarty3TemplateRepair
18
 *
19
 * Scanner process to look for and repair 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 Smarty3TemplateRepair 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 = array();
39
40
    /**
41
     * @var array replacement patterns
42
     */
43
    protected $replacements = array();
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
        $this->patterns[] = '/("<{[a-zA-Z0-9_\-]+)\s+([a-zA-Z\/\.][\/\.a-zA-Z0-9]*)\s?}>/';
68
        $this->replacements[] = "$1 '$2'}>";
69
70
        $this->patterns[] = "/('<{[a-zA-Z0-9_\-]+)\s+([a-zA-Z\/\.][\/\.a-zA-Z0-9]*)\s?}>/";
71
        $this->replacements[] = '$1 "$2"}>';
72
73
        $this->patterns[] = '/(<{[a-zA-Z0-9_\-]+)\s+([a-zA-Z\/\.][\/\.a-zA-Z0-9]*)\s?}>/';
74
        $this->replacements[] = "$1 '$2'}>";
75
    }
76
77
    /**
78
     * @param SplFileInfo $fileInfo
79
     * @return void
80
     */
81
    public function inspectFile(SplFileInfo $fileInfo)
82
    {
83
        $output = $this->output;
84
        if (false===$fileInfo->isWritable()) {
85
            return;
86
        }
87
        $length = $fileInfo->getSize();
88
        $file = $fileInfo->openFile('r+');
89
        $lines = $file->fread($length);
90
91
        $count = 0;
92
        $updatedLines = preg_replace(
93
            $this->patterns,
94
            $this->replacements,
95
            $lines,
96
            -1,
97
            $count
98
        );
99
        if ($updatedLines===null) {
100
            error;
0 ignored issues
show
Bug introduced by
The constant Xoops\Upgrade\error was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
101
        }
102
103
        /* rewrite if changes were made */
104
        if ($count !== 0) {
105
            $file->fseek(0);
106
            $file->ftruncate(0);
107
            $result = $file->fwrite($updatedLines);
108
            if ($result===false) {
109
                error;
110
            }
111
            $filename = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname());
112
            $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

112
            $output->outputIssue($output->/** @scrutinizer ignore-call */ makeOutputIssue($filename, $count));
Loading history...
113
        }
114
    }
115
}
116