SalesCest::ensureICanSeeBulkBillSearchBox()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\tests\acceptance\seller;
12
13
use hipanel\helpers\Url;
14
use hipanel\tests\_support\Page\IndexPage;
15
use hipanel\tests\_support\Page\Widget\Input\Dropdown;
16
use hipanel\tests\_support\Page\Widget\Input\Input;
17
use hipanel\tests\_support\Page\Widget\Input\Select2;
18
use hipanel\tests\_support\Step\Acceptance\Seller;
19
20
class SalesCest
21
{
22
    /**
23
     * @var IndexPage
24
     */
25
    private $index;
26
27
    public function _before(Seller $I)
28
    {
29
        $this->index = new IndexPage($I);
30
    }
31
32 View Code Duplication
    public function ensureIndexPageWorks(Seller $I)
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...
33
    {
34
        $I->login();
35
        $I->needPage(Url::to('@sale'));
36
        $I->see('Sales', 'h1');
37
        $this->ensureICanSeeAdvancedSearchBox($I);
38
        $this->ensureICanSeeBulkBillSearchBox();
39
    }
40
41
    private function ensureICanSeeAdvancedSearchBox(Seller $I)
42
    {
43
        $this->index->containsFilters([
44
            Select2::asAdvancedSearch($I, 'Seller'),
45
            Select2::asAdvancedSearch($I, 'Buyer'),
46
            (Dropdown::asAdvancedSearch($I,'Object type'))->withItems([
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class hipanel\tests\_support\P...get\Input\TestableInput as the method withItems() does only exist in the following sub-classes of hipanel\tests\_support\P...get\Input\TestableInput: hipanel\tests\_support\Page\Widget\Input\Dropdown. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
47
                'Servers',
48
                'IP',
49
                'Accounts',
50
                'Clients',
51
                'Parts',
52
            ]),
53
            Select2::asAdvancedSearch($I, 'Tariff'),
54
            Input::asAdvancedSearch($I, 'Object'),
55
        ]);
56
    }
57
58
    private function ensureICanSeeBulkBillSearchBox()
59
    {
60
        $this->index->containsBulkButtons([
61
            'Delete',
62
        ]);
63
        $this->index->containsColumns([
64
            'Object',
65
            'Seller',
66
            'Buyer',
67
            'Tariff',
68
            'Time',
69
        ]);
70
    }
71
}
72