Registryfile::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\News;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    XOOPS Project (https://xoops.org)
17
 * @license      GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
18
 * @author       XOOPS Development Team
19
 */
20
21
/**
22
 * Class Registryfile
23
 */
24
class Registryfile
25
{
26
    public $filename; // filename to manage
27
28
    /**
29
     * @param null $fichier
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fichier is correct as it would always require null to be passed?
Loading history...
30
     */
31
    public function __construct($fichier = null)
32
    {
33
        $this->setfile($fichier);
34
    }
35
36
    /**
37
     * @param null $fichier
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fichier is correct as it would always require null to be passed?
Loading history...
38
     */
39
    public function setfile($fichier = null): void
40
    {
41
        if ($fichier) {
0 ignored issues
show
introduced by
$fichier is of type null, thus it always evaluated to false.
Loading history...
42
            $this->filename = XOOPS_UPLOAD_PATH . '/' . $fichier;
43
        }
44
    }
45
46
    /**
47
     * @param null $fichier
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fichier is correct as it would always require null to be passed?
Loading history...
48
     *
49
     * @return bool|string
50
     */
51
    public function getfile($fichier = null)
52
    {
53
        $fw = '';
54
        if (!$fichier) {
0 ignored issues
show
introduced by
$fichier is of type null, thus it always evaluated to false.
Loading history...
55
            $fw = $this->filename;
56
        } else {
57
            $fw = XOOPS_UPLOAD_PATH . '/' . $fichier;
58
        }
59
        if (\file_exists($fw)) {
60
            return file_get_contents($fw);
61
        }
62
63
        return '';
64
    }
65
66
    /**
67
     * @param      $content
68
     * @param null $fichier
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fichier is correct as it would always require null to be passed?
Loading history...
69
     *
70
     * @return bool
71
     */
72
    public function savefile($content, $fichier = null)
73
    {
74
        $fw = '';
75
        if (!$fichier) {
0 ignored issues
show
introduced by
$fichier is of type null, thus it always evaluated to false.
Loading history...
76
            $fw = $this->filename;
77
        } else {
78
            $fw = XOOPS_UPLOAD_PATH . '/' . $fichier;
79
        }
80
        if (\is_file($fw)) {
81
            @\unlink($fw);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

81
            /** @scrutinizer ignore-unhandled */ @\unlink($fw);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
82
        }
83
        /** @var resource $fp */
84
        $fp = \fopen($fw, 'wb') || exit(_ERRORS);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
85
        \fwrite($fp, $content);
86
        \fclose($fp);
87
88
        return true;
89
    }
90
}
91