Td   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 93
Duplicated Lines 21.51 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 24
c 3
b 1
f 0
lcom 0
cbo 0
dl 20
loc 93
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A _init() 0 6 1
F write() 20 83 23

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
//use Fluent\Logger\FluentLogger;
4
5
namespace Fluentd\Log;
6
7
class Td extends \Fuel\Core\Log {
8
9
	public static function _init() {
10
11
		\Fluent\Autoloader::register();
12
		
13
		parent::_init();
14
	}
15
16
	public static function write($level, $msg, $method = null)
17
	{
18
		$log_threshold = \Config::get('log_threshold');
19
		$config = \Config::get('log', array());
20
21
		if (isset($config['drivers']['td']['log_threshold']))
22
		{
23
			$log_threshold = $config['drivers']['td']['log_threshold'];
24
		}
25
		if ($level > $log_threshold)
26
		{
27
			return false;
28
		}
29
		$levels = array(
30
			1  => 'Error',
31
			2  => 'Warning',
32
			3  => 'Debug',
33
			4  => 'Info',
34
		);
35
		$level = isset($levels[$level]) ? $levels[$level] : $level;
36
37
		if (\Config::get('profiling'))
38
		{
39
			\Console::log($method.' - '.$msg);
40
		}
41
42
		$host     = empty($config['drivers']['td']['host'])     ? null      : $config['drivers']['td']['host'];
43
		$port     = empty($config['drivers']['td']['port'])     ? null      : $config['drivers']['td']['port'];
44
		$options  = empty($config['drivers']['td']['options'])  ? array()   : $config['drivers']['td']['options'];
45
		$packer   = empty($config['drivers']['td']['packer'])   ? null      : $config['drivers']['td']['packer'];
46
		$database = empty($config['drivers']['td']['database']) ? 'default' : $config['drivers']['td']['database'];
47
48
		$logger = new \Fluent\Logger\FluentLogger($host,$port,$options,$packer);
49
50
		$message  = array();
51
52
		$call = array();
53
		if ( ! empty($method))
54
		{
55
			$call['method'] = $method;
56
		}else{
57
			$backtrace = debug_backtrace();
58
			$i=0;
59 View Code Duplication
			for(;$i<count($backtrace);$i++){
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
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...
60
				$backtrace[$i]['object'] = null;
61
				$break = false;
62
63
				if(isset($backtrace[$i]['class'])){
64
					if(!strstr($backtrace[$i]['class'],__NAMESPACE__)
65
						and !strstr($backtrace[$i]['class'],'Fuel\Core\Log')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
66
	
67
						//
68
						if($level === 'Error'){
69
							$msg = print_r($backtrace,true);
70
						}
71
						$break = true;
72
					}
73
				}
74
75
				if($break){
76
					break;
77
				}
78
			}
79
			if(isset($backtrace[$i])){
80
				$call['class']    = isset($backtrace[$i]['class'])    ? $backtrace[$i]['class']    : 'null';
81
				$call['type']     = isset($backtrace[$i]['type'])     ? $backtrace[$i]['type']     : 'null';
82
				$call['function'] = isset($backtrace[$i]['function']) ? $backtrace[$i]['function'] : 'null';
83
				$call['line']     = isset($backtrace[$i-1]['line'])   ? $backtrace[$i-1]['line']   : 'null';
84
			}
85
		}
86
87
		$message['level'] = $level;
88
		$message['date']  = date(\Config::get('log_date_format'));
89
		$message['msg']   = $msg;
90
	   	$message['call']  = $call;
91
92
		$res = $logger->post('td.'.$database.'.fuel_log',$message);
93
		if(!$res){
94
			return false;
95
		}
96
97
		return true;
98
	}
99
}
100