ProcessingFilter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 52.81 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 2
dl 47
loc 89
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B filter() 47 47 7
A generateCode() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * \AppserverIo\Doppelgaenger\StreamFilters\ProcessingFilter
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Bernhard Wick <[email protected]>
15
 * @copyright 2015 TechDivision GmbH - <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/doppelgaenger
18
 * @link      http://www.appserver.io/
19
 */
20
21
namespace AppserverIo\Doppelgaenger\StreamFilters;
22
23
use AppserverIo\Doppelgaenger\Entities\Definitions\FunctionDefinition;
24
use AppserverIo\Doppelgaenger\Entities\Lists\TypedListList;
25
use AppserverIo\Doppelgaenger\Exceptions\GeneratorException;
26
use AppserverIo\Doppelgaenger\Dictionaries\Placeholders;
27
use AppserverIo\Doppelgaenger\Dictionaries\ReservedKeywords;
28
29
/**
30
 * This filter will buffer the input stream and add all postcondition related information at prepared locations
31
 * (see $dependencies)
32
 *
33
 * @author    Bernhard Wick <[email protected]>
34
 * @copyright 2015 TechDivision GmbH - <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/appserver-io/doppelgaenger
37
 * @link      http://www.appserver.io/
38
 */
39
class ProcessingFilter extends AbstractFilter
40
{
41
    /**
42
     * @const integer FILTER_ORDER Order number if filters are used as a stack, higher means below others
43
     */
44
    const FILTER_ORDER = 00;
45
46
    /**
47
     * @var array $dependencies Other filters on which we depend
48
     */
49
    protected $dependencies = array('SkeletonFilter');
50
51
    /**
52
     * The main filter method.
53
     * Implemented according to \php_user_filter class. Will loop over all stream buckets, buffer them and perform
54
     * the needed actions.
55
     *
56
     * @param resource $in       Incoming bucket brigade we need to filter
57
     * @param resource $out      Outgoing bucket brigade with already filtered content
58
     * @param integer  $consumed The count of altered characters as buckets pass the filter
59
     * @param boolean  $closing  Is the stream about to close?
60
     *
61
     * @throws \AppserverIo\Doppelgaenger\Exceptions\GeneratorException
62
     *
63
     * @return integer
64
     *
65
     * @link http://www.php.net/manual/en/php-user-filter.filter.php
66
     */
67 View Code Duplication
    public function filter($in, $out, &$consumed, $closing)
68
    {
69
        // Get our buckets from the stream
70
        while ($bucket = stream_bucket_make_writeable($in)) {
71
            // Get the tokens
72
            $tokens = token_get_all($bucket->data);
73
74
            // Go through the tokens and check what we found
75
            $tokensCount = count($tokens);
76
            for ($i = 0; $i < $tokensCount; $i++) {
77
                // Did we find a function? If so check if we know that thing and insert the code of its preconditions
78
                if (is_array($tokens[$i]) && $tokens[$i][0] === T_FUNCTION && is_array($tokens[$i + 2])) {
79
                    // Get the name of the function
80
                    $functionName = $tokens[$i + 2][1];
81
82
                    // Check if we got the function in our list, if not continue
83
                    $functionDefinition = $this->params->get($functionName);
84
85
                    if (!$functionDefinition instanceof FunctionDefinition) {
86
                        continue;
87
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
88
                    } else {
89
                        // Get the code for the needed call
90
                        $code = $this->generateCode($functionDefinition);
91
92
                        // Insert the code
93
                        $bucket->data = str_replace(
94
                            Placeholders::AROUND_JOINPOINT . $functionDefinition->getName() .
95
                            Placeholders::PLACEHOLDER_CLOSE,
96
                            $code,
97
                            $bucket->data
98
                        );
99
100
                        // "Destroy" code and function definition
101
                        $code = null;
0 ignored issues
show
Unused Code introduced by
$code is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
102
                        $functionDefinition = null;
0 ignored issues
show
Unused Code introduced by
$functionDefinition is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
103
                    }
104
                }
105
            }
106
107
            // Tell them how much we already processed, and stuff it back into the output
108
            $consumed += $bucket->datalen;
109
            stream_bucket_append($out, $bucket);
110
        }
111
112
        return PSFS_PASS_ON;
113
    }
114
115
    /**
116
     * Will generate the code to call the original method logic
117
     *
118
     * @param \AppserverIo\Doppelgaenger\Entities\Definitions\FunctionDefinition $functionDefinition The function
119
     *
120
     * @return string
121
     */
122
    protected function generateCode(FunctionDefinition $functionDefinition)
123
    {
124
        // Build up the call to the original function
125
        return ReservedKeywords::RESULT . ' = ' . $functionDefinition->getHeader('call', ReservedKeywords::ORIGINAL_FUNCTION_SUFFIX, false, true) . ';';
126
    }
127
}
128