Passed
Push — master ( fdf07c...5b75cb )
by Michael
13:50 queued 04:10
created

Smarty3TemplateRepair   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 37
c 2
b 0
f 0
dl 0
loc 84
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A inspectFile() 0 32 5
A loadPatterns() 0 15 1
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
// 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
76
    /**
77
     * @param SplFileInfo $fileInfo
78
     * @return void
79
     */
80
    public function inspectFile(SplFileInfo $fileInfo)
81
    {
82
        $output = $this->output;
83
        if (false===$fileInfo->isWritable()) {
84
            return;
85
        }
86
        $length = $fileInfo->getSize();
87
        $file = $fileInfo->openFile('r+');
88
        $lines = $file->fread($length);
89
90
        $count = 0;
91
        $updatedLines = preg_replace(
92
            $this->patterns,
93
            $this->replacements,
94
            $lines,
95
            -1,
96
            $count
97
        );
98
        if ($updatedLines===null) {
99
            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...
100
        }
101
102
        /* rewrite if changes were made */
103
        if ($count !== 0) {
104
            $file->fseek(0);
105
            $file->ftruncate(0);
106
            $result = $file->fwrite($updatedLines);
107
            if ($result===false) {
108
                error;
109
            }
110
            $filename = str_replace(XOOPS_ROOT_PATH, '', $fileInfo->getPathname());
111
            $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

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