ManPage::__construct()   D
last analyzed

Complexity

Conditions 9
Paths 10

Size

Total Lines 27
Code Lines 17

Duplication

Lines 2
Ratio 7.41 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 2
loc 27
ccs 0
cts 22
cp 0
rs 4.909
c 0
b 0
f 0
cc 9
eloc 17
nc 10
nop 1
crap 90
1
<?php
2
3
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix\Plugin;
14
15
use Apix\View\View;
16
use Apix\Service;
17
18
/**
19
 * Adds a human-friendly API Manual.
20
 *
21
 * @author Franck Cassedanne <franck at ouarz.net>
22
 */
23
class ManPage extends PluginAbstract
24
{
25
26
    public static $hook = array(
27
        'response',
28
        'early',
29
        'interface' => 'Apix\View\Template\Adapter'
30
    );
31
32
    protected $options = array(
33
        'enable'    => true,    // wether to enable or not
34
35
        // Append the given string to the named group annotation. 
36
        'example'   => null,
37
        'see'       => null,
38
        'link'      => null,
39
        'copyright' => 'Powered by APIx-server, copyright (C) 2010 Franck Cassedanne.',
40
        'license'   => null,
41
        
42
        'view_dir'  => null,    // to set the view dir.
43
        'rel_path'  => '/help', // the relative path to help (no version prefix)
44
        'templater' => 'Apix\View\Template\Mustache', // the template adapter
45
46
        // Anything below is automatically populated (extracted).
47
        'version'  => 'v1', // the version string (default value).
48
        'url_api'  => null, // the API absolute URL.
49
        'url_help' => null, // the Manual absolute URL (url_api+rel_path).
50
    );
51
52
    /**
53
     * Constructor.
54
     *
55
     * @param array $options Array of options.
56
     */
57
    public function __construct(array $options = null)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
58
    {
59
        if ( !isset($options['enable']) || $options['enable']) {
60
61
            if (!isset($options['url_api'])) {
62
63
                // review thsi
64
                $uri = isset($_SERVER['SCRIPT_URI'])
65
                        ? $_SERVER['SCRIPT_URI']
66
                        : $_SERVER['REQUEST_URI'];
67
68
                preg_match(
69
                    '@^(.*(/?v[0-9]+))'
70
                    . preg_quote($this->options['rel_path'])
71
                    . '(.+)?$@i',
72
                    $uri,
73
                    $m
74
                );
75
76 View Code Duplication
                if(isset($m[1]) && !empty($m[1])) $options['url_api'] =  $m[1];
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
77 View Code Duplication
                if(isset($m[2]) && !empty($m[2])) $options['version'] =  $m[2];
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
78
            }
79
        }
80
        $this->setOptions($options);
0 ignored issues
show
Documentation introduced by
$options is of type array, but the function expects a object<Apix\Plugin\mix>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
        $this->options['url_help'] = $this->options['url_api']
82
                                     . $this->options['rel_path'];
83
    }
84
85
    public function update(\SplSubject $response)
86
    {
87
        if (
88
            false === $this->options['enable']
89
            || 'html' !== $response->getFormat()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SplSubject as the method getFormat() does only exist in the following implementations of said interface: Apix\Response.

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...
90
        ) {
91
            return false;
92
        }
93
94
        $type = key($response->results);
0 ignored issues
show
Bug introduced by
Accessing results on the interface SplSubject 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...
95
96
        if ($type == 'error') {
97
            $response->results[$type]['items'] = array();
0 ignored issues
show
Bug introduced by
Accessing results on the interface SplSubject 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...
98
        }
99
100
        $view_model = '\Apix\View\ViewModel\\' . ucfirst($type);
101
        $view = new View(new $view_model(), $response->results[$type]);
0 ignored issues
show
Bug introduced by
Accessing results on the interface SplSubject 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...
102
103
        $config = Service::get('config');
104
        $view->getViewModel()->set('config', $config);
105
        if (null === $this->options['view_dir']) {
106
            $distrib_path = $config['distrib_path'];
107
            $this->options['view_dir'] = $distrib_path . '/../templates/html';
108
        }
109
        $view->getViewModel()->set('options', $this->options);
110
111
        $view->setTemplate(
112
            $this->options['templater'],
113
            array('view_dir' => $this->options['view_dir'])
114
        );
115
116
        $response->setOutput(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SplSubject as the method setOutput() does only exist in the following implementations of said interface: Apix\Response.

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...
117
            $view->render()
118
        );
119
    }
120
121
    /**
122
     * TODO
123
     * $uri = $_SERVER['SCRIPT_URI']
124
     * $rel_path = $this->options['rel_path']
125
     */
126
    public static function getUrlApiAndVersion($uri, $rel_path)
127
    {
128
        preg_match(
129
            '@^(.*(/?v[0-9]+))' . preg_quote($rel_path) . '@i',
130
            $uri,
131
            $m
132
        );
133
134
        return $m;
135
    }
136
137
}
138