Completed
Push — master ( 6452b0...d576ea )
by Michael
05:58 queued 03:02
created

news_registryfile::getfile()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 5
Ratio 35.71 %
Metric Value
dl 5
loc 14
rs 9.4285
cc 3
eloc 10
nc 4
nop 1
1
<?php
2
//  ------------------------------------------------------------------------ //
3
//                  Copyright (c) 2005-2006 Herve Thouzard                     //
4
//                     <http://www.herve-thouzard.com/>                      //
5
// ------------------------------------------------------------------------- //
6
//  This program is free software; you can redistribute it and/or modify     //
7
//  it under the terms of the GNU General Public License as published by     //
8
//  the Free Software Foundation; either version 2 of the License, or        //
9
//  (at your option) any later version.                                      //
10
//                                                                           //
11
//  You may not change or alter any portion of this comment or credits       //
12
//  of supporting developers from this source code or any supporting         //
13
//  source code which is considered copyrighted (c) material of the          //
14
//  original comment or credit authors.                                      //
15
//                                                                           //
16
//  This program is distributed in the hope that it will be useful,          //
17
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
18
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
19
//  GNU General Public License for more details.                             //
20
//                                                                           //
21
//  You should have received a copy of the GNU General Public License        //
22
//  along with this program; if not, write to the Free Software              //
23
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
24
//  ------------------------------------------------------------------------ //
25
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
26
27
/**
28
 * Class news_registryfile
29
 */
30
class news_registryfile
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
31
{
32
    public $filename; // filename to manage
33
34
    /**
35
     * @param null $fichier
36
     */
37
    public function __construct($fichier = null)
38
    {
39
        $this->setfile($fichier);
40
    }
41
42
    /**
43
     * @param null $fichier
44
     */
45
    public function setfile($fichier = null)
46
    {
47
        if ($fichier) {
48
            $this->filename = XOOPS_UPLOAD_PATH . '/' . $fichier;
49
        }
50
    }
51
52
    /**
53
     * @param null $fichier
54
     *
55
     * @return bool|string
56
     */
57
    public function getfile($fichier = null)
58
    {
59
        $fw = '';
0 ignored issues
show
Unused Code introduced by
$fw is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60 View Code Duplication
        if (!$fichier) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
            $fw = $this->filename;
62
        } else {
63
            $fw = XOOPS_UPLOAD_PATH . '/' . $fichier;
64
        }
65
        if (file_exists($fw)) {
66
            return file_get_contents($fw);
67
        } else {
68
            return '';
69
        }
70
    }
71
72
    /**
73
     * @param      $content
74
     * @param null $fichier
75
     *
76
     * @return bool
77
     */
78
    public function savefile($content, $fichier = null)
79
    {
80
        $fw = '';
0 ignored issues
show
Unused Code introduced by
$fw is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81 View Code Duplication
        if (!$fichier) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
            $fw = $this->filename;
83
        } else {
84
            $fw = XOOPS_UPLOAD_PATH . '/' . $fichier;
85
        }
86
        if (file_exists($fw)) {
87
            @unlink($fw);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
88
        }
89
        $fp = fopen($fw, 'w') || die(_ERRORS);
0 ignored issues
show
Coding Style Compatibility introduced by
The method savefile() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
90
        fwrite($fp, $content);
91
        fclose($fp);
92
93
        return true;
94
    }
95
}
96