File   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 108
Duplicated Lines 18.52 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
F write() 20 104 24

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 Fluentd\Log;
4
5
class File extends \Fluentd\Log
6
{
7
8
	public static function write($level, $msg, $method = null)
9
	{
10
		$log_threshold = \Config::get('log_threshold');
11
		$config = \Config::get('log',array());
12
13
		if( isset($config['drivers']['file']['log_threshold']))
14
		{
15
			$log_threshold = $config['drivers']['file']['log_threshold'];
16
		}
17
		if ($level > $log_threshold)
18
		{
19
			return false;
20
		}
21
		$levels = array(
22
			1  => 'Error',
23
			2  => 'Warning',
24
			3  => 'Debug',
25
			4  => 'Info',
26
		);
27
		$level = isset($levels[$level]) ? $levels[$level] : $level;
28
29
		if (\Config::get('profiling'))
30
		{
31
			\Console::log($method.' - '.$msg);
32
		}
33
34
		$filepath = \Config::get('log_path').date('Y/m').'/';
35
36
		if ( ! is_dir($filepath))
37
		{
38
			$old = umask(0);
39
40
			mkdir($filepath, \Config::get('file.chmod.folders', 0777), true);
41
			umask($old);
42
		}
43
44
		$filename = $filepath.date('Y-m-d').'_log';
45
46
		$message  = '';
47
48
		if ( ! $exists = file_exists($filename))
49
		{
50
			$message .= "<"."?php defined('COREPATH') or exit('No direct script access allowed'); ?".">".PHP_EOL.PHP_EOL;
51
		}
52
53
		if ( ! $fp = @fopen($filename, 'a'))
54
		{
55
			return false;
56
		}
57
58
		$call = '';
59
		if ( ! empty($method))
60
		{
61
			$call .= $method;
62
		}else{
63
			$backtrace = debug_backtrace();
64
			$i=0;
65 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...
66
				$backtrace[$i]['object'] = null;
67
				$break = false;
68
69
				if(isset($backtrace[$i]['class'])){
70
					if(!strstr($backtrace[$i]['class'],__NAMESPACE__)
71
						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...
72
	
73
						//
74
						if($level === 'Error'){
75
							if ($level == 'Error') var_dump($backtrace);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($backtrace); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
76
						}
77
						$break = true;
78
					}
79
				}
80
81
				if($break){
82
					break;
83
				}
84
			}
85
			if(isset($backtrace[$i])){
86
				$call .= isset($backtrace[$i]['class'])    ? $backtrace[$i]['class']      : ' - ';
87
				$call .= isset($backtrace[$i]['type'])     ? $backtrace[$i]['type']       : ' - ';
88
				$call .= isset($backtrace[$i]['function']) ? $backtrace[$i]['function']   : ' - ';
89
				$call .= isset($backtrace[$i-1]['line'])   ? ':'.$backtrace[$i-1]['line'] : ' - ';
90
			}
91
		}
92
93
		$message .= $level.' '.(($level == 'info') ? ' -' : '-').' ';
94
		$message .= date(\Config::get('log_date_format'));
95
		$message .= ' - ' . 'ouid=' . parent::$opensocial_user_id;
96
		$message .= ' --> '.(empty($call) ? '' : $call.' - ').$msg.PHP_EOL;
97
98
		flock($fp, LOCK_EX);
99
		fwrite($fp, $message);
100
		flock($fp, LOCK_UN);
101
		fclose($fp);
102
103
		if ( ! $exists)
104
		{
105
			$old = umask(0);
106
			@chmod($filename, \Config::get('file.chmod.files', 0666));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
107
			umask($old);
108
		}
109
110
		return true;
111
	}
112
}
113