Completed
Push — master ( a12146...64a44a )
by Joao
02:25
created

PThreadHandler::__construct()   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 0
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
    private $hasError;
24
25
    /**
26
     * Thread constructor.
27
     */
28
    public function __construct()
29
    {
30
        $this->getLoader();
31
    }
32
33
    /**
34
     * @return ClassLoader
35
     */
36
    public function getLoader()
37
    {
38
        if (!is_null($this->loader)) {
39
            return $this->loader;
40
        }
41
42
        $path = __DIR__ . '/../../vendor/autoload.php';
43
        if (!file_exists($path)) {
44
            $path = __DIR__ . '/../../../../autoload.php';
45
            if (!file_exists($path)) {
46
                throw new \RuntimeException("Autoload path '$path' not found");
47
            }
48
        }
49
        $this->loader = require("$path");
50
51
        return $this->loader;
52
    }
53
54
    protected function threadError()
55
    {
56
        $this->hasError = error_get_last();
57
    }
58
59
    /**
60
     * Here you are in a threaded environment
61
     */
62
    public function run()
63
    {
64
        register_shutdown_function([$this, 'threadError']);
65
66
        $this->getLoader()->register();
67
68
        $this->result = call_user_func_array($this->callable, $this->args);
69
    }
70
71
    /**
72
     * Start the thread
73
     *
74
     * @throws \RuntimeException
75
     */
76
    public function execute()
77
    {
78
        $this->args = func_get_args();
79
        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...
80
    }
81
82
    /**
83
     * Get the thread result
84
     *
85
     * @return mixed
86
     * @throws \RuntimeException
87
     */
88
    public function getResult()
89
    {
90
        if ($this->hasError && ( $this->hasError['type'] == E_ERROR || $this->hasError['type'] == E_USER_ERROR )) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->hasError of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
91
            throw new \RuntimeException(
92
                sprintf(
93
                    'Thread error: "%s", in "%s" at line %d. <<--- ',
94
                    $this->hasError['message'],
95
                    $this->hasError['file'],
96
                    $this->hasError['line']
97
                )
98
            );
99
        }
100
        return $this->result;
101
    }
102
103
    /**
104
     * Kill a thread
105
     *
106
     * @param int $signal
107
     * @param bool $wait
108
     */
109
    public function stop($signal = SIGKILL, $wait = false)
110
    {
111
        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...
112
    }
113
114
    /**
115
     * Checkif the thread is not Terminated
116
     *
117
     * @return bool
118
     */
119
    public function isAlive()
120
    {
121
        if ($this->isRunning()) {
122
            return true;
123
        }
124
125
        if (!$this->isJoined()) {
126
            $this->join();
127
        }
128
129
        return false;
130
    }
131
132
    /**
133
     * Set the thread callable method
134
     * @param callable $callable
135
     * @return mixed
136
     */
137
    public function setCallable(callable $callable)
138
    {
139
        $this->callable = $callable;
140
    }
141
142
    public function waitFinish()
143
    {
144
        $this->join();
145
    }
146
}
147