IndexController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 42.11 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 16
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 16 2
A contactsAction() 16 16 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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