IndexController::indexAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Page\Controller;
4
5
use Application\Mvc\Controller;
6
use Page\Model\Helper\PageHelper;
7
use Page\Model\Page;
8
use Phalcon\Mvc\Dispatcher\Exception;
9
10
class IndexController extends Controller
11
{
12
13
    public function indexAction()
14
    {
15
        $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...
16
        
17
        $pageHelper = new PageHelper();
18
        $pageResult = $pageHelper->pageBySlug($slug);
19
        if (!$pageResult) {
20
            throw new Exception("Page '$slug.html' not found");
21
        }
22
23
        $this->helper->title()->append($pageResult->meta_title);
24
        $this->helper->meta()->set('description', $pageResult->meta_description);
25
        $this->helper->meta()->set('keywords', $pageResult->meta_keywords);
26
27
        $this->view->text = $pageResult->text;
0 ignored issues
show
Bug introduced by
Accessing text 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...
28
    }
29
30 View Code Duplication
    public function contactsAction()
31
    {
32
        $pageHelper = new PageHelper();
33
        $pageResult = $pageHelper->pageBySlug('contacts');
34
        if (!$pageResult) {
35
            throw new Exception("Page 'contacts' not found");
36
        }
37
38
        $this->helper->title()->append($pageResult->meta_title);
39
        $this->helper->meta()->set('description', $pageResult->meta_description);
40
        $this->helper->meta()->set('keywords', $pageResult->meta_keywords);
41
42
        $this->helper->menu->setActive('contacts');
43
44
        $this->view->text = $pageResult->text;
0 ignored issues
show
Bug introduced by
Accessing text 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...
45
    }
46
47
}
48