Completed
Push — master ( 79ead5...6b29e3 )
by Joao
12s
created

PThreadHandler::getResult()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 8
Ratio 57.14 %

Importance

Changes 0
Metric Value
dl 8
loc 14
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 2
nop 0
1
<?php
2
3
namespace ByJG\PHPThread\Handler;
4
5
use Composer\Autoload\ClassLoader;
6
7
class PThreadHandler extends \Thread implements ThreadInterface
8
{
9
    /**
10
     * @var ClassLoader
11
     */
12
    private $loader;
13
14
    /**
15
     * @var callable
16
     */
17
    private $callable;
18
19
    private $args;
20
21
    private $result;
22
23
    /**
24
     * Thread constructor.
25
     */
26
    public function __construct()
27
    {
28
        $this->getLoader();
29
    }
30
31
    /**
32
     * @return ClassLoader
33
     */
34
    public function getLoader()
35
    {
36
        if (!is_null($this->loader)) {
37
            return $this->loader;
38
        }
39
40
        $path = __DIR__ . '/../../vendor/autoload.php';
41
        if (!file_exists($path)) {
42
            $path = __DIR__ . '/../../../../autoload.php';
43
            if (!file_exists($path)) {
44
                throw new \RuntimeException("Autoload path '$path' not found");
45
            }
46
        }
47
        $this->loader = require("$path");
48
49
        return $this->loader;
50
    }
51
52
    /**
53
     * Here you are in a threaded environment
54
     */
55
    public function run()
56
    {
57
        $this->getLoader()->register();
58
59
        $callable = $this->callable;
60
        if (!is_string($callable)) {
61
            $callable = (array) $this->callable;
62
        }
63
64
        try {
65
            $this->result = call_user_func_array($callable, (array)$this->args);
66
        // Executed only in PHP 7, will not match in PHP 5.x
67
        } catch (\Throwable $ex) {
0 ignored issues
show
Bug introduced by
The class Throwable 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...
68
            $this->result = $ex;
69
        // Executed only in PHP 5. Remove when PHP 5.x is no longer necessary.
70
        } catch (\Exception $ex) {
71
            $this->result = $ex;
72
        }
73
    }
74
75
    /**
76
     * Start the thread
77
     *
78
     * @throws \RuntimeException
79
     */
80
    public function execute()
81
    {
82
        $this->args = func_get_args();
83
        return parent::start();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (start() instead of execute()). Are you sure this is correct? If so, you might want to change this to $this->start().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
84
    }
85
86
    /**
87
     * Get the thread result
88
     *
89
     * @return mixed
90
     * @throws \Error
91
     * @throws \Throwable
92
     */
93
    public function getResult()
94
    {
95
        $result = $this->result;
96 View Code Duplication
        if (is_object($result) &&
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...
97
            ($result instanceof \Exception
98
                || $result instanceof \Throwable
99
                || $result instanceof \Error
0 ignored issues
show
Bug introduced by
The class Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
100
            )
101
        ) {
102
            throw $result;
103
        }
104
105
        return $result;
106
    }
107
108
    /**
109
     * Kill a thread
110
     *
111
     * @param int $signal
112
     * @param bool $wait
113
     */
114
    public function stop($signal = SIGKILL, $wait = false)
115
    {
116
        parent::kill();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (kill() instead of stop()). Are you sure this is correct? If so, you might want to change this to $this->kill().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
117
    }
118
119
    /**
120
     * Checkif the thread is not Terminated
121
     *
122
     * @return bool
123
     */
124
    public function isAlive()
125
    {
126
        if ($this->isRunning()) {
127
            return true;
128
        }
129
130
        if (!$this->isJoined()) {
131
            $this->join();
132
        }
133
134
        return false;
135
    }
136
137
    /**
138
     * Set the thread callable method
139
     * @param callable $callable
140
     * @return mixed
141
     */
142
    public function setCallable(callable $callable)
143
    {
144
        $this->callable = $callable;
145
    }
146
147
    public function waitFinish()
148
    {
149
        $this->join();
150
    }
151
}
152