GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AbstractContent   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 225
Duplicated Lines 13.33 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 33
lcom 2
cbo 2
dl 30
loc 225
rs 9.3999
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A init() 0 1 1
A getInstance() 0 9 2
B getContent() 0 21 5
A getCurlContent() 0 18 2
A getFileContent() 14 14 2
A getStreamContent() 16 16 2
A putContent() 0 14 4
A putFileContent() 0 8 2
A putFopen() 0 6 1
D stream_notification_callback() 0 31 10
A progressCallback() 0 10 1

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
 * Created by PhpStorm.
4
 * User: pedro
5
 * Date: 24/11/16
6
 * Time: 17:19
7
 */
8
9
namespace Classes\Update\Content;
10
11
use Classes\Update\ProgressBar;
12
use Classes\Update\ProtocolFileContent;
13
14
abstract class AbstractContent
15
{
16
17
    private static $opts = array (
18
        'http' => array (
19
            'method' => 'GET' ,
20
            'header' => array (
21
                'User-Agent: PHP'
22
            )
23
        )
24
    );
25
26
    /**
27
     * @type \Classes\Update\ProtocolFileContent
28
     */
29
    protected $objProtocol;
30
    /**
31
     * @type Content
32
     */
33
    private static $objContent;
34
35
    /**
36
     * @type array
37
     */
38
    private $content = array ();
39
40
    protected function __construct ()
41
    {
42
        $this->objProtocol = ProtocolFileContent::getInstance ();
43
        $this->init ();
44
    }
45
46
    protected function init (){ }
47
48
    /**
49
     * @return \Classes\Update\Content
50
     */
51
    public static function getInstance ()
52
    {
53
        if ( empty( self::$objContent ) )
54
        {
55
            self::$objContent = new static();
0 ignored issues
show
Documentation Bug introduced by
It seems like new static() of type this<Classes\Update\Content\AbstractContent> is incompatible with the declared type object<Classes\Update\Content\Content> of property $objContent.

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...
56
        }
57
58
        return self::$objContent;
59
    }
60
61
    /**
62
     * @param $url
63
     *
64
     * @return string
65
     */
66
    public function getContent ( $url , $progress = false )
67
    {
68
        if ( ! isset( $this->content[ $url ] ) )
69
        {
70
            $this->content[ $url ] = '';
71
            switch ( $this->objProtocol->getProtocol () )
72
            {
73
                case 'curl':
74
                    $this->content[ $url ] = $this->getCurlContent ( $url , $progress );
75
                    break;
76
                case 'file_content':
77
                    $this->content[ $url ] = $this->getFileContent ( $url , $progress );
78
                    break;
79
                case 'steam_content':
80
                    $this->content[ $url ] = $this->getStreamContent ( $url , $progress );
81
                    break;
82
            }
83
        }
84
85
        return $this->content[ $url ];
86
    }
87
88
    /**
89
     * @param $url
90
     *
91
     * @return string
92
     */
93
    protected function getCurlContent ( $url , $progress = false )
94
    {
95
        $conn = curl_init ( $url );
96
        curl_setopt ( $conn , CURLOPT_RETURNTRANSFER , true );
97
        curl_setopt ( $conn , CURLOPT_BINARYTRANSFER , true );
98
        curl_setopt ( $conn , CURLOPT_USERAGENT , self::$opts[ 'http' ][ 'method' ] );
99
        if ( $progress )
100
        {
101
            curl_setopt ( $conn , CURLOPT_NOPROGRESS , false );
102
            curl_setopt ( $conn , CURLOPT_PROGRESSFUNCTION , array (
103
                $this , 'progressCallback'
104
            ) );
105
        }
106
        $url_get_contents_data = ( curl_exec ( $conn ) );
107
        curl_close ( $conn );
108
109
        return $url_get_contents_data;
110
    }
111
112
    /**
113
     * @param $url
114
     *
115
     * @return string
116
     */
117 View Code Duplication
    protected function getFileContent ( $url , $progress = false )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
118
    {
119
        $context = stream_context_create ( self::$opts );
120
        if ( $progress )
121
        {
122
            stream_context_set_params ( $context , array (
123
                "notification" => array (
124
                    $this , 'stream_notification_callback'
125
                )
126
            ) );
127
        }
128
129
        return file_get_contents ( $url , false , $context );
130
    }
131
132
    /**
133
     * @param $url
134
     *
135
     * @return string
136
     */
137 View Code Duplication
    protected function getStreamContent ( $url , $progress = false )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
138
    {
139
        $context = stream_context_create ( self::$opts );
140
        if ( $progress )
141
        {
142
            stream_context_set_params ( $context , array (
143
                "notification" => array (
144
                    $this , 'stream_notification_callback'
145
                )
146
            ) );
147
        }
148
149
        $handle = fopen ( $url , "r" , null , $context );
150
151
        return stream_get_contents ( $handle );
152
    }
153
154
    public function putContent ( $url , $content )
155
    {
156
        switch ( $this->objProtocol->getProtocol () )
157
        {
158
            case 'curl':
159
                $this->putFopen ( $url , $content );
160
                break;
161
            case 'steam_content':
162
            case 'file_content':
163
                $this->putFileContent ( $url , $content );
164
                break;
165
        }
166
167
    }
168
169
    public function putFileContent ( $url , $content )
170
    {
171
        // check if all is OK
172
        if ( file_put_contents ( $url , $content ) )
173
        {
174
            ProgressBar::getInstance ()->finish ();
175
        }
176
    }
177
178
    public function putFopen ( $url , $content )
179
    {
180
        $fp = fopen ( $url , "a" );
181
        fwrite ( $fp , $content );
182
        fclose ( $fp );
183
    }
184
185
    /**
186
     * Stream downloading
187
     *
188
     * @param $notification_code
189
     * @param $severity
190
     * @param $message
191
     * @param $message_code
192
     * @param $bytes_transferred
193
     * @param $bytes_max
194
     */
195
    public function stream_notification_callback ( $notificationCode , $severity , $message , $messageCode , $bytesTransferred , $bytesMax )
196
    {
197
        $objProgress = ProgressBar::getInstance ();
198
        switch ( $notificationCode )
199
        {
200
            case STREAM_NOTIFY_RESOLVE:
201
            case STREAM_NOTIFY_AUTH_REQUIRED:
202
            case STREAM_NOTIFY_FAILURE:
203
            case STREAM_NOTIFY_AUTH_RESULT:
204
                var_dump ( $notificationCode , $severity , $message , $messageCode , $bytesTransferred , $bytesMax );
0 ignored issues
show
Security Debugging Code introduced by
var_dump($notificationCo...ransferred, $bytesMax); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
205
                /* Ignore */
206
                break;
207
            case STREAM_NOTIFY_CONNECT:
208
                echo "\033[1;32mConnected...\033[0m\n";
209
                break;
210
            case STREAM_NOTIFY_REDIRECTED:
211
                $objProgress->clear ();
212
                break;
213
            case STREAM_NOTIFY_FILE_SIZE_IS:
214
                $objProgress->clear ()->setMaxByte ( $bytesMax );
215
                break;
216
            case STREAM_NOTIFY_PROGRESS:
217
                $objProgress->setProgress ( $bytesTransferred )
218
                            ->render ();
219
                break;
220
            case STREAM_NOTIFY_COMPLETED:
221
                $objProgress->finish ();
222
                break;
223
        }
224
225
    }
226
227
    function progressCallback ( $download_size , $downloaded_size , $upload_size , $uploaded_size )
0 ignored issues
show
Unused Code introduced by
The parameter $upload_size is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $uploaded_size is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
228
    {
229
        ProgressBar::getInstance ()
230
                   ->clear ()
231
                   ->setMaxByte ( $download_size )
232
                   ->setProgress ( $downloaded_size )
233
                   ->render ()
234
                   ->finish ();
235
236
    }
237
238
}