Completed
Push — master ( ca1336...ce2897 )
by Adrien
07:43
created

Sort::sort()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 4
eloc 9
c 3
b 1
f 0
nc 8
nop 5
dl 0
loc 16
ccs 8
cts 9
cp 0.8889
crap 4.0218
rs 9.2
1
<?php
2
3
namespace mQueue\View\Helper;
4
5
use Zend_View_Helper_Abstract;
6
7
class Sort extends Zend_View_Helper_Abstract
8
{
9
    /**
10
     * Return an HTML links to be able to sort (will typically be in table header)
11
     * @param string $label
12
     * @param string $column
13
     * @param string $selectedSortKey
14
     * @param string $selectedSortOrder
15
     * @param array $additionalParameters
0 ignored issues
show
Documentation introduced by
Should the type for parameter $additionalParameters not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
16
     * @return string
17
     */
18 1
    public function sort($label, $column, $selectedSortKey, $selectedSortOrder, array $additionalParameters = null)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
19
    {
20 1
        if (is_null($additionalParameters)) {
21
            $additionalParameters = [];
22
        }
23
24 1
        $orders = ['desc' => 'asc', 'asc' => 'desc'];
25 1
        if (!in_array($selectedSortOrder, $orders)) {
26 1
            $selectedSortOrder = reset($orders);
27
        }
28
29 1
        $url = $this->view->urlParams(array_merge($additionalParameters, ['sort' => $column, 'sortOrder' => $orders[$selectedSortOrder]]));
1 ignored issue
show
Bug introduced by
The method urlParams() does not seem to exist on object<Zend_View_Interface>.

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.

Loading history...
30 1
        $result = '<a class="sort ' . ($column == $selectedSortKey ? $selectedSortOrder : '') . '" title="' . $this->view->escape($this->view->translate('Sort by "%s"', [$label])) . '" href="' . $url . '">' . $this->view->escape($label) . '</a>';
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend_View_Interface as the method translate() does only exist in the following implementations of said interface: Zend_View.

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...
31
32 1
        return $result;
33
    }
34
}
35