Completed
Branch master (55a138)
by Michael
03:21
created

EtablissementHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 29.82 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getEtablissement() 17 17 3
A getAll() 0 14 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 namespace XoopsModules\Extcal;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 23.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    {@link https://xoops.org/ XOOPS Project}
14
 * @license      {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package      extcal
16
 * @since
17
 * @author       XOOPS Development Team,
18
 */
19
20
//Kraven 30
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
21
// defined('XOOPS_ROOT_PATH') || exit('Restricted access.');
22
23
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
24
25
26
/**
27
 * Class EtablissementHandler.
28
 */
29
class EtablissementHandler extends ExtcalPersistableObjectHandler
30
{
31
    /**
32
     * @param $db
33
     */
34
    public function __construct(\XoopsDatabase $db)
35
    {
36
        parent::__construct($db, 'extcal_etablissement', Etablissement::class, 'id', 'nom');
37
    }
38
39
    /**
40
     * @param      $etablissementId
41
     * @param bool $skipPerm
42
     *
43
     * @return bool
44
     */
45 View Code Duplication
    public function getEtablissement($etablissementId, $skipPerm = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
getEtablissement uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
46
    {
47
        $user = $GLOBALS['xoopsUser'];
48
49
        $criteriaCompo =  new \CriteriaCompo();
50
        $criteriaCompo->add( new \Criteria('id', $etablissementId));
51
52
        if (!$skipPerm) {
53
            $this->_addCatPermCriteria($criteriaCompo, $user);
54
        }
55
        $ret = $this->getObjects($criteriaCompo);
56
        if (isset($ret[0])) {
57
            return $ret[0];
58
        } else {
59
            return false;
60
        }
61
    }
62
63
    /**
64
     * @param \CriteriaElement $criteria
0 ignored issues
show
Documentation introduced by
Should the type for parameter $criteria not be null|\CriteriaElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
65
     * @param null            $fields
66
     * @param bool            $asObject
67
     * @param bool            $id_as_key
68
     *
69
     * @return array
70
     */
71
    public function &getAll(
72
        \CriteriaElement $criteria = null,
73
        $fields = null,
74
        $asObject = true,
75
        $id_as_key = true
76
    ) //getAll($criteria = null, $asObject = false)
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
77
    {
78
        $rst = $this->getObjects($criteria, $asObject);
79
        if ($asObject) {
80
            return $rst;
81
        } else {
82
            return $this->objectToArray($rst);
83
        }
84
    }
85
}
86