Completed
Push — develop ( 4bf430...c74260 )
by
unknown
18:30
created

JobboardController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 71
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 9.1369
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0

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
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** ActionController of Jobs */
11
namespace Jobs\Controller;
12
13
use Core\Form\SearchForm;
14
use Jobs\Form\ListFilter;
15
use Zend\Mvc\Controller\AbstractActionController;
16
use Zend\Session\Container as Session;
17
use Jobs\Repository;
18
use Zend\View\Model\ViewModel;
19
20
/**
21
 * @method \Auth\Controller\Plugin\Auth auth()
22
 * @method \Core\Controller\Plugin\CreatePaginatorService paginatorService()
23
 *
24
 * Controller for jobboard actions
25
 */
26
class JobboardController extends AbstractActionController
27
{
28
    /**
29
     * @var Repository\Job $jobRepository
30
     */
31
    private $jobRepository;
32
33
    /**
34
     * Formular for searching job postings
35
     *
36
     * @var ListFilter $searchForm
37
     */
38
    private $searchForm;
39
40
    /**
41
     * Construct the jobboard controller
42
     *
43
     * @param Repository\Job $jobRepository
44
     * @param ListFilter $searchForm
0 ignored issues
show
Bug introduced by
There is no parameter named $searchForm. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
45
     */
46
    public function __construct(Repository\Job $jobRepository)
47
    {
48
        $this->jobRepository = $jobRepository;
49
    }
50
    /**
51
     * attaches further Listeners for generating / processing the output
52
     * @return $this
53
     */
54 View Code Duplication
    public function attachDefaultListeners()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
55
    {
56
        parent::attachDefaultListeners();
57
        $serviceLocator = $this->serviceLocator;
58
        $defaultServices = $serviceLocator->get('DefaultListeners');
59
        $events          = $this->getEventManager();
60
        $events->attach($defaultServices);
0 ignored issues
show
Documentation introduced by
$defaultServices is of type object|array, but the function expects a string.

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...
61
        return $this;
62
    }
63
64
    /**
65
     * List jobs
66
     *
67
     * @return ViewModel
68
     */
69
    public function indexAction()
70
    {
71
72
        $result = $this->pagination([
0 ignored issues
show
Documentation Bug introduced by
The method pagination does not exist on object<Jobs\Controller\JobboardController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
73
                'params' => ['Jobs_Board', ['q', 'page' => 1, 'l', 'd']],
74
                'form' => ['as' => 'filterForm', 'Jobs/JobboardSearch'],
75
                'paginator' => ['as' => 'jobs', 'Jobs/Board']
76
            ]);
77
//        /* @var \Zend\Http\Request $request */
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
78
//        $request          = $this->getRequest();
79
//        $params           = $request->getQuery();
80
//        $jsonFormat       = 'json' == $request->getQuery()->get('format');
81
//        $event            = $this->getEvent();
82
//        $routeMatch       = $event->getRouteMatch();
83
//        $matchedRouteName = $routeMatch->getMatchedRouteName();
84
//        $url              = $this->url()->fromRoute($matchedRouteName, array(), array('force_canonical' => true));
85
//
86
//        if (!$jsonFormat && !$request->isXmlHttpRequest()) {
87
//            $session = new Session('Jobs\Index');
88
//            $sessionKey = $this->auth()->isLoggedIn() ? 'userParams' : 'guestParams';
89
//            $sessionParams = $session[$sessionKey];
90
//            if ($sessionParams) {
91
//                foreach ($sessionParams as $key => $value) {
92
//                    $params->set($key, $params->get($key, $value));
93
//                }
94
//            }
95
//            $session[$sessionKey] = $params->toArray();
96
//
97
//            $this->searchForm->bind($params);
98
//        }
99
//
100
//        $params = $params->get('params', []);
101
//
102
//        if (isset($params['l']['data']) &&
103
//            isset($params['l']['name']) &&
104
//            !empty($params['l']['name'])) {
105
//            /* @var \Geo\Form\GeoText $geoText */
106
//            $geoText = $this->searchForm->get('params')->get('l');
107
//
108
//            $geoText->setValue($params['l']);
109
//            $params['location'] = $geoText->getValue('entity');
110
//        }
111
//
112
//        if (!isset($params['sort'])) {
113
//            $params['sort']='-date';
114
//        }
115
//
116
//        $this->searchForm->setAttribute('action', $url);
117
118
        $params['by'] = "guest";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
119
120
//        $paginator = $this->paginator('Jobs/Board', $params);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
121
122
//        $options = $this->searchForm->getOptions();
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
123
//        $options['showButtons'] = false;
124
//        $this->searchForm->setOptions($options);
125
        $organizationImageCache = $this->serviceLocator->get('Organizations\ImageFileCache\Manager');
126
127
        $result['organizationImageCache'] = $organizationImageCache;
128
129
        return $result;
130
        $return = array(
0 ignored issues
show
Unused Code introduced by
$return = array('by' => ...rganizationImageCache); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
131
            'by' => $params['by'],
132
            'jobs' => $paginator,
133
            'filterForm' => $this->searchForm,
134
            'organizationImageCache' => $organizationImageCache
135
        );
136
        $model = new ViewModel($return);
137
138
        return $model;
139
    }
140
}
141