Completed
Push — master ( 563916...ca1336 )
by Adrien
05:31
created

Bootstrap.php ➔ _tr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    public static $translator = null;
6
7 16
    protected function _initAutoload()
8
    {
9
        // Add our own action helpers
10 16
        Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/mQueue/Controller/ActionHelper', 'mQueue\\Controller\\ActionHelper\\');
11
12 16
        return Zend_Loader_AutoloaderFactory::factory([
13
                    'Zend_Loader_StandardAutoloader' => [
14 16
                        'autoregister_zf' => true,
15
                        'namespaces' => [
16 16
                            'mQueue' => APPLICATION_PATH . '/mQueue',
17 16
                        ],
18 16
                    ],
19 16
        ]);
20
    }
21
22 16
    protected function _initDoctype()
23
    {
24 16
        $this->bootstrap('view');
25 16
        $view = $this->getResource('view');
26 16
        $view->doctype(Zend_View_Helper_Doctype::XHTML1_STRICT);
27
28
        // Enable our own View Helpers
29 16
        $view->addHelperPath(APPLICATION_PATH . '/mQueue/View/Helper', 'mQueue\\View\\Helper');
30 16
    }
31
32 16
    protected function _initNavigation()
33
    {
34 16
        $this->bootstrap('view');
35 16
        $view = $this->getResource('view');
36
37 16
        $navigation = new Zend_Navigation([
38
            [
39 16
                'label' => $view->translate('Movies'),
40 16
                'controller' => 'movie',
41 16
                'route' => 'default',
42
                'pages' => [
43
                    [
44 16
                        'label' => $view->translate('Add movie'),
45 16
                        'controller' => 'movie',
46 16
                        'action' => 'add',
47 16
                        'route' => 'default',
48 16
                    ],
49
                    [
50 16
                        'label' => $view->translate('Import votes from IMDb'),
51 16
                        'controller' => 'movie',
52 16
                        'action' => 'import',
53 16
                        'route' => 'default',
54 16
                    ],
55 16
                ],
56 16
            ],
57
            [
58 16
                'label' => $view->translate('Activity'),
59 16
                'controller' => 'activity',
60 16
                'route' => 'default',
61 16
            ],
62
            [
63 16
                'label' => $view->translate('Users'),
64 16
                'controller' => 'user',
65 16
                'route' => 'default',
66 16
            ],
67
            [
68 16
                'label' => $view->translate('FAQ'),
69 16
                'controller' => 'faq',
70 16
                'route' => 'default',
71 16
            ],
72 16
        ]);
73
74 16
        $view->navigation($navigation);
75 16
    }
76
77 16
    protected function _initSession()
78
    {
79 16
        Zend_Session::setOptions(['name' => 'mqueue']);
80 16
    }
81
82 16
    protected function _initLanguage()
0 ignored issues
show
Coding Style introduced by
_initLanguage uses the super-global variable $_GET 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...
83
    {
84 16
        $session = new Zend_Session_Namespace();
85
86
        // handle language switch
87 16
        if (isset($_GET['lang'])) {
88
            $session->locale = $_GET['lang'];
89
        }
90
91 16
        if (isset($session->locale)) {
92
            $locale = new Zend_Locale($session->locale);
93
        } else {
94 16
            $locale = new Zend_Locale(); // autodetect browser
95
        }
96 16
        Zend_Registry::set('Zend_Locale', $locale);
97
98 16
        $adapter = new Zend_Translate('gettext', APPLICATION_PATH . '/localization', $locale, ['scan' => Zend_Translate::LOCALE_FILENAME, 'disableNotices' => true]);
99 16
        Zend_Registry::set('Zend_Translate', $adapter);
100 16
        Zend_Form::setDefaultTranslator($adapter);
101 16
        self::$translator = $adapter;
102 16
    }
103
104 16
    protected function _initPagination()
105
    {
106 16
        Zend_Paginator::setDefaultScrollingStyle('Elastic');
107 16
        Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
108 16
    }
109
110 16
    protected function _initRoutes()
111
    {
112 16
        $front = Zend_Controller_Front::getInstance();
113 16
        $router = $front->getRouter();
114
115
        // Required for unit tests
116 16
        $router->addDefaultRoutes();
117
118
        // A route for single id (typically view a single movie/user)
119 16
        $router->addRoute('singleid', new Zend_Controller_Router_Route(':controller/:action/:id', ['action' => 'view']));
120
121
        // A route for activities
122 16
        $router->addRoute('activity', new Zend_Controller_Router_Route('activity/*', ['controller' => 'activity', 'action' => 'index']));
123 16
        $router->addRoute('activityMovie', new Zend_Controller_Router_Route('activity/movie/:movie/*', ['controller' => 'activity', 'action' => 'index']));
124 16
        $router->addRoute('activityUser', new Zend_Controller_Router_Route('activity/user/:user/*', ['controller' => 'activity', 'action' => 'index']));
125
126
        // For backward compatibility with RSS readers we keep the old route
127 16
        $router->addRoute('activityOld', new Zend_Controller_Router_Route('activity/index/*', ['controller' => 'activity', 'action' => 'index']));
128 16
        $router->addRoute('activityMovieOld', new Zend_Controller_Router_Route('activity/index/movie/:movie/*', ['controller' => 'activity', 'action' => 'index']));
129 16
        $router->addRoute('activityUserOld', new Zend_Controller_Router_Route('activity/index/user/:user/*', ['controller' => 'activity', 'action' => 'index']));
130
131
        // Routes to define and view statuses
132 16
        $router->addRoute('status', new Zend_Controller_Router_Route_Regex('status/(\d{7})/(\d)', ['controller' => 'status', 'action' => 'index'], [1 => 'movie', 2 => 'rating'], 'status/%s/%s'));
133 16
        $router->addRoute('statusView', new Zend_Controller_Router_Route_Regex('status/(\d{7})', ['controller' => 'status', 'action' => 'index'], [1 => 'movie'], 'status/%s'));
134 16
    }
135
136
    /**
137
     * Add the Zend_Db_Adapter to the registry if we need to call it outside of the modules.
138
     * @return Zend_Db_Adapter
139
     */
140 16
    protected function _initMyDb()
141
    {
142 16
        $db = $this->getPluginResource('db')->getDbAdapter();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend_Application_Resource_Resource as the method getDbAdapter() does only exist in the following implementations of said interface: Zend_Application_Resource_Db.

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...
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
143 16
        Zend_Registry::set('db', $db);
144
145 16
        return $db;
146
    }
147
}
148
149
/**
150
 * Global shortcut method that returns localized strings
151
 *
152
 * @param string $msgId the original string to translate
153
 * @return string the translated string
154
 */
155
function _tr($msgId)
0 ignored issues
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 4 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
156
{
157 12
    return Bootstrap::$translator->translate($msgId);
158
}
159