Completed
Push — master ( b24c92...8112ea )
by Aimeos
11:37
created

DiscardFilter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A filter() 0 10 2
1
<?php
2
3
namespace Aimeos\MW\Process;
4
5
6
class PcntlTest extends \PHPUnit\Framework\TestCase
7
{
8
	protected function setUp()
9
	{
10
		if( function_exists( 'pcntl_fork' ) === false ) {
11
			$this->markTestSkipped( 'PCNTL extension not available' );
12
		}
13
	}
14
15
16
	public function testIsAvailable()
17
	{
18
		$object = new \Aimeos\MW\Process\Pcntl();
19
		$this->assertTrue( $object->isAvailable() );
20
	}
21
22
23
	public function testRun()
24
	{
25
		$object = new \Aimeos\MW\Process\Pcntl();
26
		$fcn = function() { sleep( 1 ); };
27
28
		$start = microtime( true );
29
		$return = $object->start( $fcn, [] )->start( $fcn, [] )->wait();
30
		$msec = ( microtime( true ) - $start );
31
32
		$this->assertInstanceOf( '\Aimeos\MW\Process\Iface', $return );
33
		$this->assertGreaterThan( 1, $msec );
34
		$this->assertLessThan( 2, $msec );
35
	}
36
37
38
	public function testRunException()
39
	{
40
		$fcn = function() { throw new \Exception(); };
41
42
		try
43
		{
44
			stream_filter_register( "redirect", "\Aimeos\MW\Process\DiscardFilter" );
45
			$filter = stream_filter_prepend( STDERR, "redirect", STREAM_FILTER_WRITE );
46
47
			$object = new \Aimeos\MW\Process\Pcntl();
48
			$object->start( $fcn, [], true )->wait();
49
50
			stream_filter_remove( $filter );
51
		}
52
		catch( \Aimeos\MW\Process\Exception $e )
53
		{
54
			stream_filter_remove( $filter );
0 ignored issues
show
Bug introduced by
The variable $filter does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
55
			return;
56
		}
57
58
		$this->fail( 'Expected exception "\Aimeos\MW\Process\Exception" not thrown' );
59
	}
60
}
61
62
63
64
class DiscardFilter extends \php_user_filter
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
65
{
66
	function filter( $in, $out, &$consumed, $closing )
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
	{
68
		while( $bucket = stream_bucket_make_writeable( $in ) )
69
		{
70
			$bucket->data = '';
71
			$consumed += $bucket->datalen;
72
			stream_bucket_append( $out, $bucket );
73
		}
74
		return PSFS_PASS_ON;
75
	}
76
}
77