Completed
Pull Request — 1.0 (#29)
by Nic
14:32
created

CatalogProductInquiryFormTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 50 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 24
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testCatalogProductInquiryFormFields() 12 12 2
A testCatalogProductInquiryFormActions() 12 12 2

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
 * Class CatalogProductInquiryFormTest
5
 */
6
class CatalogProductInquiryFormTest extends SapphireTest
7
{
8
9
    /**
10
     * @var string
11
     */
12
    protected static $fixture_file = 'product-catalog/tests/fixtures.yml';
13
14
    /**
15
     * @var array
16
     */
17
    protected $extraDataObjects = [
18
        'TestCatalogCategory',
19
    ];
20
21
    /**
22
     *
23
     */
24 View Code Duplication
    public function testCatalogProductInquiryFormFields()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
25
    {
26
        $form = CatalogProductInquiryForm::create(CatalogCategory_Controller::create($this->objFromFixture('CatalogCategory',
27
            'default')), 'ProductInquiryForm');
28
        $fields = $form->Fields();
29
30
        $this->assertInstanceOf('FieldList', $fields);
31
32
        foreach ($fields as $field) {
0 ignored issues
show
Bug introduced by
The expression $fields of type object<FieldList>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
33
            $this->assertInstanceOf('FormField', $field, "field {$field->getName()} isn't a FormField instance");
34
        }
35
    }
36
37
    /**
38
     *
39
     */
40 View Code Duplication
    public function testCatalogProductInquiryFormActions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
41
    {
42
        $form = CatalogProductInquiryForm::create(CatalogCategory_Controller::create($this->objFromFixture('CatalogCategory',
43
            'default')), 'ProductInquiryForm');
44
        $actions = $form->Actions();
45
46
        $this->assertInstanceOf('FieldList', $actions);
47
48
        foreach ($actions as $action) {
0 ignored issues
show
Bug introduced by
The expression $actions of type object<FieldList>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
49
            $this->assertInstanceOf('FormAction', $action, "action {$action->getName()} isn't a FormAction instance");
50
        }
51
    }
52
53
}