SitemapController   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\SitemapForm;
7
8
class SitemapController extends Controller
9
{
10
11
    private $sitemapFilePath;
12
13 View Code Duplication
    public function initialize()
14
    {
15
        $this->setAdminEnvironment();
16
        $this->helper->activeMenu()->setActive('seo-sitemap');
17
        $this->sitemapFilePath = ROOT . '/sitemap.xml';
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 SitemapForm();
24
25 View Code Duplication
        if ($this->request->isPost()) {
26
            if ($form->isValid()) {
27
                $sitemap = $this->request->getPost('sitemap');
28
                $result = file_put_contents($this->sitemapFilePath, $sitemap);
29
                if ($result) {
30
                    $this->flash->success('File sitemap.xml has been saved');
31
                    $this->redirect($this->url->get() . 'seo/sitemap');
32
                } else {
33
                    $this->flash->error('Error! The sitemap.xml file is not updated. Check the write permissions to the file.');
34
                }
35
            } else {
36
                $this->flashErrors($form);
37
            }
38
        } else {
39
            $sitemap = file_get_contents($this->sitemapFilePath);
40
            $form->get('sitemap')->setDefault($sitemap);
0 ignored issues
show
Bug introduced by
The method setDefault cannot be called on $form->get('sitemap') (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
        $title = 'Editing sitemap.xml';
44
        $this->helper->title($title);
45
        $this->view->title = $title;
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...
46
        $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...
47
    }
48
49
}
50