LoggingConfigHandler   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 139
Duplicated Lines 18.71 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 26
loc 139
rs 9
c 0
b 0
f 0
wmc 35
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
D execute() 26 115 35

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
namespace Agavi\Config;
3
4
// +---------------------------------------------------------------------------+
5
// | This file is part of the Agavi package.                                   |
6
// | Copyright (c) 2005-2011 the Agavi Project.                                |
7
// | Based on the Mojavi3 MVC Framework, Copyright (c) 2003-2005 Sean Kerr.    |
8
// |                                                                           |
9
// | For the full copyright and license information, please view the LICENSE   |
10
// | file that was distributed with this source code. You can also view the    |
11
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
12
// |   vi: set noexpandtab:                                                    |
13
// |   Local Variables:                                                        |
14
// |   indent-tabs-mode: t                                                     |
15
// |   End:                                                                    |
16
// +---------------------------------------------------------------------------+
17
18
19
use Agavi\Config\Util\Dom\XmlConfigDomElement;
20
use Agavi\Config\XmlConfigHandler;
21
use Agavi\Config\Util\Dom\XmlConfigDomDocument;
22
use Agavi\Exception\ConfigurationException;
23
24
/**
25
 * AgaviLoggingConfigHandler allows you to register loggers with the system.
26
 *
27
 * @package    agavi
28
 * @subpackage config
29
 *
30
 * @author     David Zülke <[email protected]>
31
 * @author     Dominik del Bondio <[email protected]>
32
 * @author     Bob Zoller <[email protected]>
33
 * @author     Sean Kerr <[email protected]>
34
 * @copyright  Authors
35
 * @copyright  The Agavi Project
36
 *
37
 * @since      0.10.0
38
 *
39
 * @version    $Id$
40
 */
41
class LoggingConfigHandler extends XmlConfigHandler
42
{
43
    const XML_NAMESPACE = 'http://agavi.org/agavi/config/parts/logging/1.1';
44
    
45
    /**
46
     * Execute this configuration handler.
47
     *
48
     * @param      XmlConfigDomDocument $document The document to parse.
49
     *
50
     * @return     string Data to be written to a cache file.
51
     *
52
     * @throws     <b>AgaviUnreadableException</b> If a requested configuration
53
     *                                             file does not exist or is not
54
     *                                             readable.
55
     * @throws     <b>AgaviParseException</b> If a requested configuration file is
56
     *                                        improperly formatted.
57
     *
58
     * @author     David Zülke <[email protected]>
59
     * @author     Dominik del Bondio <[email protected]>
60
     * @author     Bob Zoller <[email protected]>
61
     * @author     Sean Kerr <[email protected]>
62
     * @since      0.9.0
63
     */
64
    public function execute(XmlConfigDomDocument $document)
65
    {
66
        // set up our default namespace
67
        $document->setDefaultNamespace(self::XML_NAMESPACE, 'logging');
68
        
69
        // init our data, includes, methods, appenders and appenders arrays
70
        $code      = array();
71
        $loggers   = array();
72
        $appenders = array();
73
        $layouts   = array();
74
75
        foreach ($document->getConfigurationElements() as $cfg) {
76
            if ($cfg->has('loggers')) {
77
                /** @var XmlConfigDomElement $logger */
78
                foreach ($cfg->get('loggers') as $logger) {
79
                    $name = $logger->getAttribute('name');
80 View Code Duplication
                    if (!isset($loggers[$name])) {
0 ignored issues
show
Duplication introduced by
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...
81
                        $loggers[$name] = array('class' => null, 'level' => null, 'appenders' => array(), 'params' => array());
82
                    }
83
                    $loggers[$name]['class'] = $logger->hasAttribute('class') ? $logger->getAttribute('class') : $loggers[$name]['class'];
84
                    $loggers[$name]['level'] = $logger->hasAttribute('level') ? $logger->getAttribute('level') : $loggers[$name]['level'];
85
                    if ($logger->has('appenders')) {
86
                        /** @var XmlConfigDomElement $appender */
87
                        foreach ($logger->get('appenders') as $appender) {
88
                            $loggers[$name]['appenders'][] = $appender->getValue();
89
                        }
90
                    }
91
                    $loggers[$name]['params'] = $logger->getAgaviParameters($loggers[$name]['params']);
92
                }
93
            }
94
95
            if ($cfg->has('appenders')) {
96
                foreach ($cfg->get('appenders') as $appender) {
97
                    $name = $appender->getAttribute('name');
98 View Code Duplication
                    if (!isset($appenders[$name])) {
0 ignored issues
show
Duplication introduced by
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...
99
                        $appenders[$name] = array('class' => null, 'layout' => null, 'params' => array());
100
                    }
101
                    $appenders[$name]['class'] = $appender->hasAttribute('class') ? $appender->getAttribute('class') : $appenders[$name]['class'];
102
                    $appenders[$name]['layout'] = $appender->hasAttribute('layout') ? $appender->getAttribute('layout') : $appenders[$name]['layout'];
103
104
                    $appenders[$name]['params'] = $appender->getAgaviParameters($appenders[$name]['params']);
105
                }
106
            }
107
108
            if ($cfg->has('layouts')) {
109
                /** @var XmlConfigDomElement $layout */
110
                foreach ($cfg->get('layouts') as $layout) {
111
                    $name = $layout->getAttribute('name');
112
                    if (!isset($layouts[$name])) {
113
                        $layouts[$name] = array('class' => null, 'params' => array());
114
                    }
115
116
                    $layouts[$name]['class'] = $layout->hasAttribute('class') ? $layout->getAttribute('class') : $layouts[$name]['class'];
117
                    $layouts[$name]['params'] = $layout->getAgaviParameters($layouts[$name]['params']);
118
                }
119
            }
120
121
            if ($cfg->has('loggers')) {
122
                $defaultLogger = $cfg->getChild('loggers')->getAttribute('default');
123
                if (!isset($loggers[$defaultLogger])) {
124
                    throw new ConfigurationException(sprintf('Logger "%s" is configured as default, but does not exist.', $defaultLogger));
125
                }
126
            }
127
        }
128
129
        if (count($loggers) > 0) {
130
            foreach ($layouts as $name => $layout) {
131 View Code Duplication
                if (!isset($layout['class'])) {
0 ignored issues
show
Duplication introduced by
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...
132
                    throw new ConfigurationException(sprintf('logging.xml has no class defined for layout "%s".', $name));
133
                } elseif (!class_exists($layout['class'])) {
134
                    throw new ConfigurationException(sprintf('logging.xml specifies unknown class "%s" for layout "%s".', $layout['class'], $name));
135
                }
136
                $code[] = sprintf('${%s} = new %s();', var_export('_layout_' . $name, true), $layout['class']);
137
                $code[] = sprintf('${%s}->initialize($this->context, %s);', var_export('_layout_' . $name, true), var_export($layout['params'], true));
138
            }
139
140
            foreach ($appenders as $name => $appender) {
141 View Code Duplication
                if (!isset($appender['class'])) {
0 ignored issues
show
Duplication introduced by
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...
142
                    throw new ConfigurationException(sprintf('logging.xml has no class defined for appender "%s".', $name));
143
                } elseif (!class_exists($appender['class'])) {
144
                    throw new ConfigurationException(sprintf('logging.xml specifies unknown class "%s" for appender "%s".', $appender['class'], $name));
145
                }
146
                $code[] = sprintf('${%s} = new %s();', var_export('_appender_' . $name, true), $appender['class']);
147
                $code[] = sprintf('${%s}->initialize($this->context, %s);', var_export('_appender_' . $name, true), var_export($appender['params'], true));
148 View Code Duplication
                if (!isset($appender['layout'])) {
0 ignored issues
show
Duplication introduced by
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...
149
                    throw new ConfigurationException(sprintf('logging.xml has no layout defined for appender "%s".', $name));
150
                } elseif (!isset($layouts[$appender['layout']])) {
151
                    throw new ConfigurationException(sprintf('logging.xml specifies unknown layout "%s" for appender "%s".', $appender['layout'], $name));
152
                }
153
                $code[] = sprintf('${%s}->setLayout(${%s});', var_export('_appender_' . $name, true), var_export('_layout_' . $appender['layout'], true));
154
            }
155
156
            foreach ($loggers as $name => $logger) {
157 View Code Duplication
                if (!isset($logger['class'])) {
0 ignored issues
show
Duplication introduced by
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...
158
                    throw new ConfigurationException(sprintf('logging.xml has no class defined for logger "%s".', $name));
159
                } elseif (!class_exists($logger['class'])) {
160
                    throw new ConfigurationException(sprintf('logging.xml specifies unknown class "%s" for logger "%s".', $logger['class'], $name));
161
                }
162
                $code[] = sprintf('${%s} = new %s();', var_export('_logger_' . $name, true), $logger['class']);
163
                foreach ($logger['appenders'] as $appender) {
164
                    if (!isset($appenders[$appender])) {
165
                        throw new ConfigurationException(sprintf('logging.xml specifies unknown appender "%s" for logger "%s".', $appender, $name));
166
                    }
167
                    $code[] = sprintf('${%s}->setAppender(%s, ${%s});', var_export('_logger_' . $name, true), var_export($appender, true), var_export('_appender_' . $appender, true));
168
                }
169
                if ($logger['level'] !== null) {
170
                    $code[] = sprintf('${%s}->setLevel(%s);', var_export('_logger_' . $name, true), $logger['level']);
171
                }
172
                $code[] = sprintf('$this->setLogger(%s, ${%s});', var_export($name, true), var_export('_logger_' . $name, true));
173
            }
174
            $code[] = sprintf('$this->setDefaultLoggerName(%s);', var_export($defaultLogger, true));
0 ignored issues
show
Bug introduced by
The variable $defaultLogger does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
175
        }
176
177
        return $this->generate($code, $document->documentURI);
178
    }
179
}
180