Completed
Pull Request — master (#541)
by Richard
07:06
created

XoopsZipDownloader   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 109
Duplicated Lines 23.85 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 26
loc 109
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
wmc 13
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addFile() 13 13 4
A addBinaryFile() 13 13 4
A addFileData() 0 5 1
A addBinaryFileData() 0 5 1
A download() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
/**
13
 * XOOPS zip downloader
14
 *
15
 * @copyright       XOOPS Project (http://xoops.org)
16
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
17
 * @package         class
18
 * @since           2.0.0
19
 * @author          Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
20
 * @version         $Id$
21
 */
22
23
/**
24
 * Abstract base class for forms
25
 *
26
 * @author Kazumi Ono <[email protected]>
27
 * @author John Neill <[email protected]>
28
 * @copyright copyright (c) XOOPS.org
29
 * @package class
30
 */
31
class XoopsZipDownloader extends XoopsDownloader
32
{
33
    /**
34
     * Constructor
35
     *
36
     * @param string $ext      extention
37
     * @param string $mimyType mimi type
38
     *
39
     * @return XoopsZipDownloader
40
     */
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
41
42 1
    public function __construct($ext = '.zip', $mimyType = 'application/x-zip')
43
    {
44 1
        $this->archiver = new zipfile();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \zipfile() of type object<zipfile> is incompatible with the declared type object<XoopsDownloader> of property $archiver.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45 1
        $this->ext = trim($ext);
46 1
        $this->mimeType = trim($mimyType);
0 ignored issues
show
Bug introduced by
The property mimeType does not seem to exist. Did you mean mimetype?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
47 1
    }
48
49
    /**
50
     * Add file
51
     *
52
     * @param string $filepath    path
53
     * @param string $newfilename name
54
     *
55
     * @return false|null
56
     */
57 View Code Duplication
    public function addFile($filepath, $newfilename = null)
58
    {
59
        // Read in the file's contents
60
        $fp = @fopen($filepath, "r");
61
        if ($fp === false) {
62
            return false;
63
        }
64
        $data = fread($fp, filesize($filepath));
65
        fclose($fp);
66
        $filename = (isset($newfilename) && trim($newfilename) != '') ? trim($newfilename) : $filepath;
67
        $result = $this->archiver->addFile($data, $filename, filemtime($filename));
0 ignored issues
show
Unused Code introduced by
The call to XoopsDownloader::addFile() has too many arguments starting with filemtime($filename).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
Are you sure the assignment to $result is correct as $this->archiver->addFile..., filemtime($filename)) (which targets XoopsDownloader::addFile()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
68
        return $result;
69
    }
70
71
    /**
72
     * Add Binary File
73
     *
74
     * @param string $filepath    path
75
     * @param string $newfilename name
76
     *
77
     * @return false|null
78
     */
79 View Code Duplication
    public function addBinaryFile($filepath, $newfilename = null)
80
    {
81
        // Read in the file's contents
82
        $fp = @fopen($filepath, "rb");
83
        if ($fp === false) {
84
            return false;
85
        }
86
        $data = fread($fp, filesize($filepath));
87
        fclose($fp);
88
        $filename = (isset($newfilename) && trim($newfilename) != '') ? trim($newfilename) : $filepath;
89
        $result = $this->archiver->addFile($data, $filename, filemtime($filename));
0 ignored issues
show
Unused Code introduced by
The call to XoopsDownloader::addFile() has too many arguments starting with filemtime($filename).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
Are you sure the assignment to $result is correct as $this->archiver->addFile..., filemtime($filename)) (which targets XoopsDownloader::addFile()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
90
        return $result;
91
    }
92
93
    /**
94
     * Add File Data
95
     *
96
     * @param string &$data    data
97
     * @param string $filename name
98
     * @param int    $time     time
99
     *
100
     * @return result
101
     */
102
    public function addFileData(&$data, $filename, $time = 0)
103
    {
104
        $result = $this->archiver->addFile($data, $filename, $time);
0 ignored issues
show
Unused Code introduced by
The call to XoopsDownloader::addFile() has too many arguments starting with $time.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
Are you sure the assignment to $result is correct as $this->archiver->addFile($data, $filename, $time) (which targets XoopsDownloader::addFile()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
105
        return $result;
106
    }
107
108
    /**
109
     * Add Binary File Data
110
     *
111
     * @param string &$data    data
112
     * @param string $filename name
113
     * @param int    $time     file time
114
     *
115
     * @return result|null
116
     */
117
    public function addBinaryFileData(&$data, $filename, $time = 0)
118
    {
119
        $result = $this->addFileData($data, $filename, $time);
120
        return $result;
121
    }
122
123
    /**
124
     * Fownload Data as a Zip file
125
     *
126
     * @param string $name zip name
127
     * @param bool   $gzip unused
128
     *
129
     * @return void
130
     */
131
    public function download($name, $gzip = true)
132
    {
133
        $this->_header($name . $this->ext);
134
        $result = $this->archiver->file();
0 ignored issues
show
Bug introduced by
The method file() does not seem to exist on object<XoopsDownloader>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
        if ($result !== false) {
136
            echo $result;
137
        }
138
    }
139
}
140