Completed
Push — master ( 1a0eba...384b36 )
by Xu
09:28 queued 03:48
created

BaseJob::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.tintsoft.com/
4
 * @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
5
 * @license http://www.tintsoft.com/license/
6
 */
7
8
namespace yuncms\jobs;
9
10
use yii\base\BaseObject;
11
use yii\queue\RetryableJobInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, yuncms\jobs\RetryableJobInterface. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
13
/**
14
 * Class BaseJob
15
 *
16
 * @author Tongle Xu <[email protected]>
17
 * @since 3.0
18
 */
19
abstract class BaseJob extends BaseObject implements RetryableJobInterface
20
{
21
    /**
22
     * @var string|null The configured job description
23
     */
24
    public $description;
25
26
    /**
27
     * @var int The current progress
28
     */
29
    private $_progress;
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function init()
35
    {
36
        parent::init();
37
        // Set the default progress
38
        $this->_progress = 0;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function getDescription()
45
    {
46
        return $this->description ?? $this->defaultDescription();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->defaultDescription() targeting yuncms\jobs\BaseJob::defaultDescription() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
47
    }
48
49
    // Protected Methods
50
    // =========================================================================
51
52
    /**
53
     * Returns a default description for [[getDescription()]].
54
     *
55
     * @return string|null
56
     */
57
    protected function defaultDescription()
58
    {
59
        return null;
60
    }
61
62
    /**
63
     * Sets the job progress on the queue.
64
     *
65
     * @param \yii\queue\Queue|QueueInterface $queue
66
     * @param float                           $progress A number between 0 and 1
67
     */
68
    protected function setProgress($queue, float $progress)
69
    {
70
        if ($progress !== $this->_progress && $queue instanceof QueueInterface) {
0 ignored issues
show
Bug introduced by
The type yuncms\jobs\QueueInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
            $queue->setProgress(round(100 * $progress));
72
        }
73
    }
74
}