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 |
||
19 | class InterpreterTest extends TestCase |
||
20 | { |
||
21 | /** |
||
22 | * @var ContextFactory |
||
23 | */ |
||
24 | private $contextFactoryProphecy; |
||
25 | |||
26 | /** |
||
27 | * @var UqlExtensionContainer |
||
28 | */ |
||
29 | private $extensionContainerProphecy; |
||
30 | |||
31 | public function testBuildFilter() |
||
32 | { |
||
33 | $astSubtree = new ASTGroup( |
||
34 | 'T_LOGIC_AND', |
||
35 | [ |
||
36 | new ASTGroup( |
||
37 | 'T_LOGIC_OR', |
||
38 | [ |
||
39 | new ASTAssertion( |
||
40 | 'test_dse_1', |
||
41 | 'T_OP_EQ', |
||
42 | 'value1' |
||
43 | ), |
||
44 | new ASTAssertion( |
||
45 | 'test_dse_2', |
||
46 | 'T_OP_LT', |
||
47 | 'value2' |
||
48 | ), |
||
49 | ] |
||
50 | ), |
||
51 | new ASTAssertion( |
||
52 | 'test_dse_3', |
||
53 | 'T_OP_NEQ', |
||
54 | 'value3' |
||
55 | ), |
||
56 | ] |
||
57 | ); |
||
58 | |||
59 | $filterDefinition1 = new Filter(); |
||
60 | $filterDefinition1->setConditionType('OR'); |
||
61 | $filter1 = new FilterCondition('test_dse_1', FilterCondition::METHOD_NUMERIC_EQ, 'value1', 'value1'); |
||
62 | $filter2 = new FilterCondition('test_dse_2', FilterCondition::METHOD_NUMERIC_LT, 'value2', 'value2'); |
||
63 | $filterDefinition1[] = $filter1; |
||
64 | $filterDefinition1[] = $filter2; |
||
65 | $filter3 = new FilterCondition('test_dse_3', FilterCondition::METHOD_NUMERIC_NEQ, 'value3', 'value3'); |
||
66 | $filterDefinition2 = new Filter(); |
||
67 | $filterDefinition2->setConditionType('AND'); |
||
68 | $filterDefinition2[] = $filterDefinition1; |
||
69 | $filterDefinition2[] = $filter3; |
||
70 | |||
71 | $expectedFilter = $filterDefinition2; |
||
72 | |||
73 | $dataSourceElements = [ |
||
74 | new Field('test_dse_1', '', '', new NumberDataType()), |
||
75 | new Field('test_dse_2', '', '', new NumberDataType()), |
||
76 | new Field('test_dse_3', '', '', new NumberDataType()), |
||
77 | ]; |
||
78 | |||
79 | $dataSourceProphecy = $this->prophesize(DataSourceInterface::class); |
||
80 | $dataSourceProphecy->getFields()->willReturn($dataSourceElements); |
||
81 | |||
82 | $interpreterFactory = new InterpreterFactory($this->extensionContainerProphecy->reveal(), new FilterConditionFactory(), $this->contextFactoryProphecy->reveal()); |
||
83 | $interpreter = $interpreterFactory->create($dataSourceProphecy->reveal()); |
||
84 | |||
85 | $actualFilter = $interpreter->buildFilter($astSubtree); |
||
86 | $this->assertEquals($expectedFilter, $actualFilter); |
||
87 | } |
||
88 | |||
89 | View Code Duplication | public function testTranslateOperator() |
|
90 | { |
||
91 | // Manually set the data source element, mimic what the data source would do |
||
92 | $dataSourceElement = new Field( |
||
93 | 'test_data_source_element_name', |
||
94 | '', |
||
95 | '', |
||
96 | new StringDataType() |
||
97 | ); |
||
98 | |||
99 | $dataSourceProphecy = $this->prophesize(DataSourceInterface::class); |
||
100 | $dataSourceProphecy->getFields()->willReturn([$dataSourceElement]); |
||
101 | |||
102 | $interpreterFactory = new InterpreterFactory($this->extensionContainerProphecy->reveal(), new FilterConditionFactory(), $this->contextFactoryProphecy->reveal()); |
||
103 | $interpreter = $interpreterFactory->create($dataSourceProphecy->reveal()); |
||
104 | |||
105 | // LIKE is valid for String type, should return LIKE |
||
106 | $this->assertEquals(FilterCondition::METHOD_STRING_LIKE, $interpreter->translateOperator('T_OP_LIKE', $dataSourceElement), 'Failed to translate T_OP_LIKE into STRING_LIKE for type String'); |
||
107 | |||
108 | // EQ is valid for String, and should choose STRING_EQ as it's the default for the type |
||
109 | $this->assertEquals(FilterCondition::METHOD_STRING_EQ, $interpreter->translateOperator('T_OP_EQ', $dataSourceElement), 'Failed to translate T_OP_EQ into STRING_EQ for type String'); |
||
110 | } |
||
111 | |||
112 | View Code Duplication | public function testInStatementsOnlyAcceptArraysAsValues() |
|
113 | { |
||
114 | $astSubtree = new ASTAssertion('field', 'T_OP_IN', '123'); |
||
115 | $field = new Field('field', '', '', new NumberDataType()); |
||
116 | |||
117 | $dataSourceProphecy = $this->prophesize(DataSourceInterface::class); |
||
118 | $dataSourceProphecy->getFields()->willReturn([$field]); |
||
119 | |||
120 | $interpreterFactory = new InterpreterFactory($this->extensionContainerProphecy->reveal(), new FilterConditionFactory(), $this->contextFactoryProphecy->reveal()); |
||
121 | $interpreter = $interpreterFactory->create($dataSourceProphecy->reveal()); |
||
122 | |||
123 | $this->expectException(UQLInterpreterException::class); |
||
124 | $this->expectExceptionMessage('Only arrays are valid arguments for IN / NOT IN statements'); |
||
125 | $interpreter->buildFilter($astSubtree); |
||
126 | } |
||
127 | |||
128 | View Code Duplication | public function testNullValueCanBeCheckedOnlyWithIsOperator() |
|
129 | { |
||
130 | $astSubtree = new ASTAssertion('field', 'T_OP_GT', null); |
||
131 | $field = new Field('field', '', '', new NumberDataType()); |
||
132 | |||
133 | $dataSourceProphecy = $this->prophesize(DataSourceInterface::class); |
||
134 | $dataSourceProphecy->getFields()->willReturn([$field]); |
||
135 | |||
136 | $interpreterFactory = new InterpreterFactory($this->extensionContainerProphecy->reveal(), new FilterConditionFactory(), $this->contextFactoryProphecy->reveal()); |
||
137 | $interpreter = $interpreterFactory->create($dataSourceProphecy->reveal()); |
||
138 | |||
139 | $this->expectException(UQLInterpreterException::class); |
||
140 | $this->expectExceptionMessage('Only IS / IS NOT operator can be used to compare against null value'); |
||
141 | $interpreter->buildFilter($astSubtree); |
||
142 | } |
||
143 | |||
144 | protected function setUp() |
||
149 | } |
||
150 |
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.