These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | require_once('vendor/autoload.php'); |
||
3 | |||
4 | // Method to be executed in a thread |
||
5 | View Code Duplication | function Foo($t) |
|
0 ignored issues
–
show
|
|||
6 | { |
||
7 | echo "Starting thread #$t" . PHP_EOL; |
||
8 | |||
9 | sleep(1 * rand(1, 5)); |
||
10 | for ($i = 0; $i < 10; $i++) { |
||
11 | echo "Hello from thread #$t, i=$i" . PHP_EOL; |
||
12 | sleep(1); |
||
13 | } |
||
14 | echo "Ending thread #$t" . PHP_EOL; |
||
15 | |||
16 | // Note: this line below require the file "config/cacheconfig.php" exists |
||
17 | return "$t: [[[[[[" . time() . "]]]]]]"; |
||
18 | } |
||
19 | |||
20 | try { |
||
21 | $t = array(); |
||
22 | |||
23 | // Create the threads |
||
24 | for ($i = 0; $i < 10; $i++) { |
||
25 | // Create a new instance of the Thread class, pointing to "Foo" function |
||
26 | $thr = new \ByJG\PHPThread\Thread('Foo'); |
||
27 | |||
28 | // Started the method "Foo" in a tread |
||
29 | $thr->execute($i); |
||
30 | |||
31 | // Save the thread reference to be manipulate |
||
32 | $t[] = $thr; |
||
33 | } |
||
34 | |||
35 | $done = false; |
||
36 | |||
37 | // It is important to check if all threads are done |
||
38 | // otherwise will be terminate when the php script is finished; |
||
39 | foreach ($t as $thread) { |
||
40 | $thread->waitFinish(); |
||
41 | } |
||
42 | |||
43 | // Note: this line below require the file "config/cacheconfig.php" exists |
||
44 | foreach ($t as $thread) { |
||
45 | echo "Result: " . $thread->getResult() . "\n"; |
||
46 | } |
||
47 | |||
48 | } catch (Exception $e) { |
||
49 | echo 'Exception: ' . $e . PHP_EOL; |
||
50 | } |
||
51 |
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.