ConfigureBuy   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 39
ccs 0
cts 32
cp 0
rs 10
c 1
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 11 4
A uomSelection() 0 4 1
A builders() 0 19 2
1
<?php
2
3
namespace SpeckCatalog\View\Helper;
4
5
use Zend\View\Helper\AbstractHelper;
6
7
class ConfigureBuy extends AbstractHelper
8
{
9
    public function __invoke($product, $formId)
10
    {
11
        if ($product->has('uoms') && count($product->getUoms()) > 1) {
12
            $this->uomSelection();
13
        }
14
        if ($product->getProductTypeId() == 1) {
15
            $this->uomSelection();
16
            $this->builders($product->getBuilders(), $formId);
17
        }
18
        return;
19
    }
20
21
    public function uomSelection()
22
    {
23
        $this->getView()->uomSelection = true;
0 ignored issues
show
Bug introduced by
Accessing uomSelection on the interface Zend\View\Renderer\RendererInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
24
    }
25
26
    public function builders($builders, $formId)
27
    {
28
        $data = array();
29
        foreach ($builders as $builder) {
30
            $data[$builder->getProductId()] = array_values($builder->getSelected());
31
        }
32
        $builderJson = json_encode($data);
33
34
        $script = <<<js
35
var builders = {$builderJson};
36
jQuery('document').ready(function(){
37
    uomBuilders(jQuery('form#{$formId}'), builders);
38
});
39
js;
40
41
        $this->getView()->headScript()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\View\Renderer\RendererInterface as the method headScript() does only exist in the following implementations of said interface: Zend\View\Renderer\PhpRenderer.

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...
42
            ->appendFile('/js/configure-buy.js')
43
            ->appendScript($script);
44
    }
45
}
46