Completed
Push — master ( 28f97d...964a34 )
by Aimeos
03:23
created

View::create()   C

Complexity

Conditions 9
Paths 66

Size

Total Lines 58
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 2 Features 3
Metric Value
c 10
b 2
f 3
dl 0
loc 58
rs 6.9928
cc 9
eloc 36
nc 66
nop 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://www.gnu.org/copyleft/lgpl.html
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package flow
7
 * @subpackage Base
8
 */
9
10
11
namespace Aimeos\Shop\Base;
12
13
use TYPO3\Flow\Annotations as Flow;
14
15
16
/**
17
 * Class providing the view objects
18
 *
19
 * @package flow
20
 * @subpackage Base
21
 * @Flow\Scope("singleton")
22
 */
23
class View
24
{
25
	/**
26
	 * @var \Aimeos\Shop\Base\I18n
27
	 * @Flow\Inject
28
	 */
29
	protected $i18n;
30
31
32
	/**
33
	 * Creates the view object for the HTML client.
34
	 *
35
	 * @param \Aimeos\MW\Config\Iface $config Config object
36
	 * @param \TYPO3\Flow\Mvc\Routing\UriBuilder $uriBuilder URL builder object
37
	 * @param array $templatePaths List of base path names with relative template paths as key/value pairs
38
	 * @param \TYPO3\Flow\Mvc\RequestInterface|null $request Request object
39
	 * @param string|null $langid Language ID
40
	 * @return \Aimeos\MW\View\Iface View object
41
	 */
42
	public function create( \Aimeos\MW\Config\Iface $config,
43
		\TYPO3\Flow\Mvc\Routing\UriBuilder $uriBuilder, array $templatePaths,
44
		\TYPO3\Flow\Mvc\RequestInterface $request = null, $langid = null )
45
	{
46
		$params = $fixed = array();
47
48
		if( $request !== null && $langid !== null )
49
		{
50
			$params = $request->getArguments();
51
			$fixed = $this->getFixedParams( $request );
52
53
			$i18n = $this->i18n->get( array( $langid ) );
54
			$translation = $i18n[$langid];
55
		}
56
		else
57
		{
58
			$translation = new \Aimeos\MW\Translation\None( 'en' );
59
		}
60
61
62
		$view = new \Aimeos\MW\View\Standard( $templatePaths );
63
64
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
65
		$view->addHelper( 'translate', $helper );
66
67
		$helper = new \Aimeos\MW\View\Helper\Url\Flow( $view, $uriBuilder, $fixed );
68
		$view->addHelper( 'url', $helper );
69
70
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
71
		$view->addHelper( 'param', $helper );
72
73
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
74
		$view->addHelper( 'config', $helper );
75
76
		$sepDec = $config->get( 'client/html/common/format/seperatorDecimal', '.' );
77
		$sep1000 = $config->get( 'client/html/common/format/seperator1000', ' ' );
78
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $sepDec, $sep1000 );
79
		$view->addHelper( 'number', $helper );
80
81
		if( $request !== null )
82
		{
83
			$req = $request->getHttpRequest();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TYPO3\Flow\Mvc\RequestInterface as the method getHttpRequest() does only exist in the following implementations of said interface: TYPO3\Flow\Mvc\ActionRequest.

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...
84
85
			$files = ( is_array( $_FILES ) ? $_FILES : array() );
86
			$query = ( is_array( $_GET ) ? $_GET : array() );
87
			$post = ( is_array( $_POST ) ? $_POST : array() );
88
			$cookie = ( is_array( $_COOKIE ) ? $_COOKIE : array() );
89
			$server = ( is_array( $_SERVER ) ? $_SERVER : array() );
90
91
			$helper = new \Aimeos\MW\View\Helper\Request\Flow( $view, $req, $files, $query, $post, $cookie, $server );
92
			$view->addHelper( 'request', $helper );
93
		}
94
95
		$helper = new \Aimeos\MW\View\Helper\Response\Flow( $view );
96
		$view->addHelper( 'response', $helper );
97
98
		return $view;
99
	}
100
101
102
	/**
103
	 * Returns the fixed parameters that should be included in every URL
104
	 *
105
	 * @param \TYPO3\Flow\Mvc\RequestInterface $request Request object
106
	 * @return array Associative list of site, language and currency if available
107
	 */
108
	protected function getFixedParams( \TYPO3\Flow\Mvc\RequestInterface $request )
109
	{
110
		$fixed = array();
111
112
		$params = $request->getArguments();
113
114
		if( isset( $params['site'] ) ) {
115
			$fixed['site'] = $params['site'];
116
		}
117
118
		if( isset( $params['locale'] ) ) {
119
			$fixed['locale'] = $params['locale'];
120
		}
121
122
		if( isset( $params['currency'] ) ) {
123
			$fixed['currency'] = $params['currency'];
124
		}
125
126
		return $fixed;
127
	}
128
}