Passed
Push — master ( f982b7...ed48f7 )
by Joao
02:03
created

PThreadHandler::getClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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) {
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();
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
        if (is_object($result) &&
97
            ($result instanceof \Exception
98
                || $result instanceof \Throwable
99
                || $result instanceof \Error
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
Bug introduced by
The method kill() does not exist on Thread. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        parent::/** @scrutinizer ignore-call */ 
117
                kill();

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...
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
        if ($this->isAlive()) {
151
            $this->waitFinish();
152
        }
153
    }
154
155
    public function getClassName()
156
    {
157
        return PThreadHandler::class;
158
    }
159
}
160