IndexController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 82
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 54 5
A publicationAction() 0 22 3
1
<?php
2
3
namespace Publication\Controller;
4
5
use Application\Mvc\Controller;
6
use Publication\Model\Helper\PublicationHelper;
7
use Publication\Model\Publication;
8
use Phalcon\Exception;
9
use Publication\Model\Type;
10
11
class IndexController extends Controller
12
{
13
14
    public function indexAction()
15
    {
16
        $type      = $this->dispatcher->getParam('type', 'string');
0 ignored issues
show
Bug introduced by
The method getParam does only exist in Phalcon\Mvc\Dispatcher, but not in Phalcon\Mvc\DispatcherInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
17
        $typeModel = Type::getCachedBySlug($type);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $typeModel is correct as \Publication\Model\Type::getCachedBySlug($type) (which targets Publication\Model\Type::getCachedBySlug()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
18
        if (!$typeModel) {
19
            throw new Exception("Publication hasn't type = '$type''");
20
        }
21
22
        $typeLimit = ($typeModel->getLimit()) ? $typeModel->getLimit() : 10;
23
        $limit     = $this->request->getQuery('limit', 'string', $typeLimit);
24
        if ($limit != 'all') {
25
            $paginatorLimit = (int)$limit;
26
        } else {
27
            $paginatorLimit = 9999;
28
        }
29
        $page = $this->request->getQuery('page', 'int', 1);
30
31
        /*$publications = Publication::find(array(
32
            "type_id = {$typeModel->getId()}",
33
            "order" => "date DESC",
34
        ));*/
35
36
        $publicationHelper = new PublicationHelper();
37
        $fields = $publicationHelper->translateFieldsSubQuery();
38
39
        $columns = ['p.*', 't_slug' => 't.slug'];
40
        $columns = array_merge($columns, $fields);
41
42
        $qb = $this->modelsManager->createBuilder()
43
            ->columns($columns)
44
            ->addFrom('Publication\Model\Publication', 'p')
45
            ->leftJoin('Publication\Model\Type', null, 't')
46
            ->andWhere('t.slug = :type:', ['type' => $type])
47
            ->andWhere('p.date <= NOW()')
48
            ->orderBy('p.date DESC');
49
50
        $paginator = new \Phalcon\Paginator\Adapter\QueryBuilder([
51
            "builder"  => $qb,
52
            "limit" => $paginatorLimit,
53
            "page"  => $page
54
        ]);
55
56
        $this->view->paginate = $paginator->getPaginate();
0 ignored issues
show
Bug introduced by
Accessing paginate on the interface Phalcon\Mvc\ViewInterface 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...
Bug introduced by
Are you sure the assignment to $this->view->paginate is correct as $paginator->getPaginate() (which targets Phalcon\Paginator\Adapte...yBuilder::getPaginate()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
57
58
        $this->helper->title()->append($typeModel->getHeadTitle());
59
        if ($page > 1) {
60
            $this->helper->title()->append($this->helper->translate('Страница №') . ' ' . $page);
61
        }
62
        $this->view->title  = $typeModel->getTitle();
0 ignored issues
show
Bug introduced by
Accessing title on the interface Phalcon\Mvc\ViewInterface 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...
63
        $this->view->format = $typeModel->getFormat();
0 ignored issues
show
Bug introduced by
Accessing format on the interface Phalcon\Mvc\ViewInterface 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...
64
        $this->view->type   = $type;
0 ignored issues
show
Bug introduced by
Accessing type on the interface Phalcon\Mvc\ViewInterface 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...
65
66
        $this->helper->menu->setActive($type);
67
    }
68
69
    public function publicationAction()
70
    {
71
        $slug = $this->dispatcher->getParam('slug', 'string');
0 ignored issues
show
Bug introduced by
The method getParam does only exist in Phalcon\Mvc\Dispatcher, but not in Phalcon\Mvc\DispatcherInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
72
        $type = $this->dispatcher->getParam('type', 'string');
73
74
        $publicationHelper = new PublicationHelper();
75
        $publicationResult = $publicationHelper->publicationBySlug($slug);
76
        if (!$publicationResult) {
77
            throw new Exception("Publication '$slug.html' not found");
78
        }
79
        if ($publicationResult->p->getTypeSlug() != $type) {
80
            throw new Exception("Publication type <> $type");
81
        }
82
83
        $this->helper->title()->append($publicationResult->meta_title);
84
        $this->helper->meta()->set('description', $publicationResult->meta_description);
85
        $this->helper->meta()->set('keywords', $publicationResult->meta_keywords);
86
87
        $this->helper->menu->setActive($type);
88
89
        $this->view->publicationResult = $publicationResult;
0 ignored issues
show
Bug introduced by
Accessing publicationResult on the interface Phalcon\Mvc\ViewInterface 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...
90
    }
91
92
}
93