Completed
Push — 2.0 ( 14c778...b5ef61 )
by Marco
11:18
created

Task   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 137
Duplicated Lines 34.31 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 47
loc 137
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 9 32 2
run() 0 1 ?
A start() 0 13 2
B execTask() 38 38 4

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 namespace Comodojo\Extender\Tasks;
2
3
use \Comodojo\Extender\Components\Parameters;
4
use \Comodojo\Dispatcher\Components\Timestamp as TimestampTrait;
5
use \Psr\Log\LoggerInterface;
6
7
/**
8
 * Task object
9
 *
10
 * @package     Comodojo extender
11
 * @author      Marco Giovinazzi <[email protected]>
12
 * @license     GPL-3.0+
13
 *
14
 * LICENSE:
15
 * 
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28
 */
29
30
abstract class Task {
31
32
    use TimestampTrait;
33
34
    public $parameters;
35
    
36
    public $name;
37
    
38
    public $jobid;
39
    
40
    public $pid;
41
    
42
    protected $configuration;
43
    
44
    protected $logger;
45
    
46
    private $worklog;
47
    
48
    private $worklog_id;
49
50
    /**
51
     * Task constructor.
52
     * 
53
     * @param   array           $parameters     Array of parameters (if any)
54
     * @param   \Monolog\Logger $logger
55
     * @param   int             $pid            Task PID (if any)
0 ignored issues
show
Bug introduced by
There is no parameter named $pid. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
56
     * @param   string          $name           Task Name
57
     * @param   int             $timestamp      Start timestamp (if null will be retrieved directly)
58
     * @param   bool            $multithread    Multithread switch
0 ignored issues
show
Bug introduced by
There is no parameter named $multithread. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
59
     * 
60
     * @return  Object  $this 
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...
61
     */
62
    final public function __construct(
63
        Configuration $configuration,
64
        LoggerInterface $logger,
65
        $name = 'EXTENDERTASK',
66
        $timestamp = null,
67
        $jobid = null,
0 ignored issues
show
Unused Code introduced by
The parameter $jobid 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...
68
        $parameters = array()
69
    ) {
70
        
71
        // Setup task
72
        $this->configuration = $configuration;
73
        $this->logger = $logger;
74
        $this->parameters = new Parameters($parameters);
75
        $this->worklog = new Worklog($configuration, $logger);
76
        
77
        $this->name = $name;
78
        $this->setTimestamp($timestamp);
79
        $this->pid = getmypid();
80
81
        // Setup an exit strategy if multithread enabled (parent may kill child process if timeout exceeded)
82
        
83 View Code Duplication
        pcntl_signal(SIGTERM, function() {
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...
84
85
            $end = microtime(true);
86
87
            if ( !is_null($this->worklog_id) ) $this->worklog->close($this->worklog_id, false, 'Job killed (timeout exceeded?)', $end);
0 ignored issues
show
Unused Code introduced by
The call to Worklog::close() has too many arguments starting with $this->worklog_id.

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...
88
89
            exit(1);
90
91
        });
92
93
    }
94
    
95
    /**
96
     * The run method; SHOULD be implemented by each task
97
     */
98
    abstract public function run();
99
    
100
    
101
    /**
102
     * Start task!
103
     * 
104
     * @return  array
105
     */
106
    final public function start() {
107
108
        try {
109
110
            return $this->execTask();
111
112
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class Comodojo\Extender\Tasks\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
113
114
            throw new TaskException($e->getMessage(), $e->getCode(), $e, $this->worklog_id);
115
            
116
        }
117
        
118
    }
119
    
120
    /**
121
     * Execute task.
122
     *
123
     * This method provides to:
124
     * - setup worklog
125
     * - invoke method "run", that should be defined in task implementation
126
     */
127 View Code Duplication
    private function execTask() {
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...
128
129
        try {
130
131
            // open worklog
132
133
            $this->worklog_id = $this->worklog->create($this->pid, $this->name, $this->class, $this->start_timestamp);
0 ignored issues
show
Bug introduced by
The property class does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property start_timestamp does not seem to exist. Did you mean timestamp?

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...
Unused Code introduced by
The call to Worklog::create() has too many arguments starting with $this->pid.

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 $this->worklog_id is correct as $this->worklog->create($...$this->start_timestamp) (which targets Comodojo\Extender\Tasks\Worklog::create()) 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...
134
135
            $this->result = $this->run();
0 ignored issues
show
Bug introduced by
The property result does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
136
137
            $this->end_timestamp = microtime(true);
0 ignored issues
show
Bug introduced by
The property end_timestamp does not seem to exist. Did you mean timestamp?

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...
138
139
            $this->closeWorklog(true);
0 ignored issues
show
Bug introduced by
The method closeWorklog() does not seem to exist on object<Comodojo\Extender\Tasks\Task>.

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...
140
141
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class Comodojo\Extender\Tasks\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
142
143
            $this->result = $e->getMessage();
144
145
            if ( !is_null($this->worklog_id) ) {
146
147
                if ( is_null($this->end_timestamp) ) $this->end_timestamp = microtime(true);
0 ignored issues
show
Bug introduced by
The property end_timestamp does not seem to exist. Did you mean timestamp?

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...
148
149
                $this->closeWorklog(false);
0 ignored issues
show
Bug introduced by
The method closeWorklog() does not seem to exist on object<Comodojo\Extender\Tasks\Task>.

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...
150
151
            }
152
153
            throw $e;
154
155
        }
156
157
        return array(
158
            "success"   => true,
159
            "timestamp" => $this->end_timestamp,
0 ignored issues
show
Bug introduced by
The property end_timestamp does not seem to exist. Did you mean timestamp?

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...
160
            "result"    => $this->result,
161
            "worklogid" => $this->worklog_id
162
        );
163
164
    }
165
    
166
}