RobotsController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 57.14 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 24
loc 42
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 7 7 1
A indexAction() 17 27 4

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 Seo\Controller;
4
5
use Application\Mvc\Controller;
6
use Seo\Form\RobotsForm;
7
8
class RobotsController extends Controller
9
{
10
11
    private $robotsFilePath;
12
13 View Code Duplication
    public function initialize()
14
    {
15
        $this->setAdminEnvironment();
16
        $this->helper->activeMenu()->setActive('seo-robots');
17
        $this->robotsFilePath = ROOT . '/robots.txt';
18
        $this->view->languages_disabled = true;
0 ignored issues
show
Bug introduced by
Accessing languages_disabled 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...
19
    }
20
21
    public function indexAction()
22
    {
23
        $form = new RobotsForm();
24
25 View Code Duplication
        if ($this->request->isPost()) {
26
            if ($form->isValid()) {
27
                $robots = $this->request->getPost('robots', 'string');
28
                $result = file_put_contents($this->robotsFilePath, $robots);
29
                if ($result) {
30
                    $this->flash->success('File robots.txt has been saved');
31
                    $this->redirect($this->url->get() . 'seo/robots');
32
                } else {
33
                    $this->flash->error('Error! The robots.txt file is not updated. Check the write permissions to the file.');
34
                }
35
            } else {
36
                $this->flashErrors($form);
37
            }
38
        } else {
39
            $robots = file_get_contents($this->robotsFilePath);
40
            $form->get('robots')->setDefault($robots);
0 ignored issues
show
Bug introduced by
The method setDefault cannot be called on $form->get('robots') (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
41
        }
42
43
        $this->helper->title('Editing robots.txt', true);
44
        $this->view->form = $form;
0 ignored issues
show
Bug introduced by
Accessing form 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
49
}
50