ModelConsole   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 17.95 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 14
loc 78
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 7 39 7
A delete() 7 21 3

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
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   Kumbia
11
 * @package    Console
12
 *
13
 * @copyright  Copyright (c) 2005 - 2019 KumbiaPHP Team (http://www.kumbiaphp.com)
14
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
15
 */
16
17
/**
18
 * Consola para manejar modelos.
19
 *
20
 * @category   Kumbia
21
 * @package    Console
22
 */
23
class ModelConsole
24
{
25
    /**
26
     * Comando de consola para crear un modelo.
27
     *
28
     * @param array  $params parametros nombrados de la consola
29
     * @param string $model  modelo
30
     * @throw KumbiaException
31
     */
32
    public function create($params, $model)
33
    {
34
        // nombre de archivo
35
        $file = APP_PATH.'models';
36
37
        // obtiene el path
38
        $path = explode('/', trim($model, '/'));
39
40
        // obtiene el nombre de modelo
41
        $model_name = array_pop($path);
42
43 View Code Duplication
        if (count($path)) {
0 ignored issues
show
Duplication introduced by joanhey
This code seems to be duplicated across 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...
44
            $dir = implode('/', $path);
45
            $file .= "/$dir";
46
            if (!is_dir($file) && !FileUtil::mkdir($file)) {
47
                throw new KumbiaException("No se ha logrado crear el directorio \"$file\"");
48
            }
49
        }
50
        $file .= "/$model_name.php";
51
52
        // si no existe o se sobreescribe
53
        if (!is_file($file) ||
54
            Console::input('El modelo existe, desea sobrescribirlo? (s/n): ', array('s', 'n')) == 's') {
55
            // nombre de clase
56
            $class = Util::camelcase($model_name);
57
58
            // codigo de modelo
59
            ob_start();
60
            include __DIR__.'/generators/model.php';
61
            $code = '<?php'.PHP_EOL.ob_get_clean();
62
63
            // genera el archivo
64
            if (file_put_contents($file, $code)) {
65
                echo "-> Creado modelo $model_name en: $file".PHP_EOL;
66
            } else {
67
                throw new KumbiaException("No se ha logrado crear el archivo \"$file\"");
68
            }
69
        }
70
    }
71
72
    /**
73
     * Comando de consola para eliminar un modelo.
74
     *
75
     * @param array  $params parametros nombrados de la consola
76
     * @param string $model  modelo
77
     * @throw KumbiaException
78
     */
79
    public function delete($params, $model)
0 ignored issues
show
Unused Code introduced by Emilio Silveira
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
    {
81
        // nombre de archivo
82
        $file = APP_PATH.'models/'.trim($model, '/');
83
84
        // si es un directorio
85 View Code Duplication
        if (is_dir($file)) {
0 ignored issues
show
Duplication introduced by joanhey
This code seems to be duplicated across 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...
86
            $success = FileUtil::rmdir($file);
87
        } else {
88
            // entonces es un archivo
89
            $file = "$file.php";
90
            $success = unlink($file);
91
        }
92
93
        // mensaje
94
        if ($success) {
95
            echo "-> Eliminado: $file".PHP_EOL;
96
        } else {
97
            throw new KumbiaException("No se ha logrado eliminar \"$file\"");
98
        }
99
    }
100
}
101