Completed
Pull Request — master (#5)
by Laurent
02:35 queued 42s
created

CreateReceiverMonthBillCommandHandler::handle()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 38
Code Lines 27

Duplication

Lines 38
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 27
nc 3
nop 1
dl 38
loc 38
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
require_once __DIR__ . '/../class/bbcvols.class.php';
4
require_once __DIR__ . '/CreateReceiverMonthBillCommand.php';
5
require_once __DIR__ . '/../../product/class/product.class.php';
6
require_once __DIR__ . '/../../compta/facture/class/facture.class.php';
7
require_once __DIR__ . '/../../user/class/user.class.php';
8
require_once __DIR__ . '/../../adherents/class/adherent.class.php';
9
require_once __DIR__ . '/AbstractBillCommandHandler.php';
10
require_once __DIR__ . '/CommandInterface.php';
11
12
/**
13
 * @author Laurent De Coninck <[email protected]>
14
 */
15
class CreateReceiverMonthBillCommandHandler extends AbstractBillCommandHandler
16
{
17
18
    /**
19
     * @param CreateReceiverMonthBillCommand|CommandInterface $command
20
     */
21 View Code Duplication
    public function handle(CommandInterface $command)
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...
22
    {
23
        $object = new Facture($this->db);
24
        $object->fetch_thirdparty();
25
26
        $object->socid = $this->getCustomer($command->getReceiverId())->id;
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CommandInterface as the method getReceiverId() does only exist in the following implementations of said interface: CreateReceiverMonthBillCommand.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
27
        $object->type = $command->getBillingType();
28
        $object->number = "provisoire";
29
        $object->date = $this->generateBillDate($command->getYear(), $command->getMonth());
30
        $object->date_pointoftax = "";
31
        $object->note_public = $command->getPublicNote();
32
        $object->note_private = $command->getPrivateNote();
33
        $object->ref_client = "";
34
        $object->ref_int = "";
35
        $object->modelpdf = $command->getModelDocument();
36
        $object->cond_reglement_id = $command->getBillingCondition();
37
        $object->mode_reglement_id = $command->getBillType();
38
        $object->fk_account = $this->getBankAccount();
39
40
        $id = $object->create($this->user);
41
42
        if ($id <= 0) {
43
            throw new \InvalidArgumentException('Error during bill creation');
44
        }
45
46
        $flightProduct = $this->getProduct();
47
        foreach ($command->getFlights() as $flight) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CommandInterface as the method getFlights() does only exist in the following implementations of said interface: CreateReceiverMonthBillCommand.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
48
            $this->addOrderLine($object, $flightProduct, $flight);
49
            $this->addLinks($object, $flight);
50
            $this->flagFlightAsBilled($flight);
51
        }
52
53
        $this->generateBillDocument($command, $object, $id);
0 ignored issues
show
Compatibility introduced by
$command of type object<CommandInterface> is not a sub-type of object<CreateReceiverMonthBillCommand>. It seems like you assume a concrete implementation of the interface CommandInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
54
55
        $this->validates($object, $id);
56
57
        $this->generateBillDocument($command, $object, $id);
0 ignored issues
show
Compatibility introduced by
$command of type object<CommandInterface> is not a sub-type of object<CreateReceiverMonthBillCommand>. It seems like you assume a concrete implementation of the interface CommandInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
58
    }
59
60
61
    /**
62
     * @return int
63
     */
64
    private function getBankAccount()
65
    {
66
        return $this->conf->BBC_DEFAULT_BANK_ACCOUNT;
67
    }
68
69
    /**
70
     * @param int $year
71
     * @param int $month
72
     *
73
     * @return int
74
     */
75
    private function generateBillDate($year, $month)
76
    {
77
        $day = cal_days_in_month(CAL_GREGORIAN, $month, $year);
78
        $date = (new DateTime())->setDate($year, $month, $day);
79
        return $date->getTimestamp();
80
    }
81
}