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

AlternateFormats::alternateFormats()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 5
eloc 13
c 3
b 1
f 0
nc 5
nop 2
dl 0
loc 24
ccs 12
cts 12
cp 1
crap 5
rs 8.5125
1
<?php
2
3
namespace mQueue\View\Helper;
4
5
use Zend_Registry;
6
use Zend_View_Helper_Abstract;
7
8
class AlternateFormats extends Zend_View_Helper_Abstract
9
{
10
    protected static $supportedFormats = [
11
        'rss' => [
12
            'name' => 'RSS',
13
        ],
14
        'csv' => [
15
            'name' => 'CSV',
16
        ],
17
    ];
18
19
    /**
20
     * Returns a string of HTML links for end-user, and also append 'alternate' links to HTML header
21
     * @param array $formats
22
     * @param string $title
0 ignored issues
show
Documentation introduced by
Should the type for parameter $title not be string|null?

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.

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

Loading history...
23
     * @return string
24
     */
25 4
    public function alternateFormats(array $formats, $title = 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...
26
    {
27 4
        $formatLinks = [];
28 4
        foreach ($formats as $format => $url) {
29
30
            // Inject format and locale parameters
31 4
            if (strpos($url, '?') === false) {
32 3
                $url .= '?';
33
            } else {
34 1
                $url .= '&';
35
            }
36
37 4
            $url .= 'format=' . $format . '&lang=' . Zend_Registry::get('Zend_Locale')->getLanguage();
38
39 4
            $formatLinks [] = '<a class="' . $format . '" href="' . $url . '">' . self::$supportedFormats[$format]['name'] . '</a>';
40 4
            if ($title && isset(self::$supportedFormats[$format]['mime'])) {
41 4
                $this->view->headLink()->appendAlternate($url, self::$supportedFormats[$format]['mime'], $title);
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 headLink() 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...
42
            }
43
        }
44
45 4
        $result = '<p class="alternateFormats">' . $this->view->translate('Also available in:') . ' ' . implode(' | ', $formatLinks) . '</p>';
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...
46
47 4
        return $result;
48
    }
49
}
50