Completed
Push — master ( 64a44a...ce5e3e )
by Joao
04:39
created

PThreadHandler::setCallable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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 \Exception
91
     */
92
    public function getResult()
93
    {
94
        $result = $this->result;
95 View Code Duplication
        if (is_object($result) && (is_subclass_of($result, '\\Error') || is_subclass_of($result, '\\Exception'))) {
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...
96
            throw $result;
97
        }
98
99
        return $result;
100
    }
101
102
    /**
103
     * Kill a thread
104
     *
105
     * @param int $signal
106
     * @param bool $wait
107
     */
108
    public function stop($signal = SIGKILL, $wait = false)
109
    {
110
        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...
111
    }
112
113
    /**
114
     * Checkif the thread is not Terminated
115
     *
116
     * @return bool
117
     */
118
    public function isAlive()
119
    {
120
        if ($this->isRunning()) {
121
            return true;
122
        }
123
124
        if (!$this->isJoined()) {
125
            $this->join();
126
        }
127
128
        return false;
129
    }
130
131
    /**
132
     * Set the thread callable method
133
     * @param callable $callable
134
     * @return mixed
135
     */
136
    public function setCallable(callable $callable)
137
    {
138
        $this->callable = $callable;
139
    }
140
141
    public function waitFinish()
142
    {
143
        $this->join();
144
    }
145
}
146