Passed
Pull Request — master (#12)
by Katsuhiro
02:04
created

File::write()   F

Complexity

Conditions 24
Paths > 20000

Size

Total Lines 104
Code Lines 61

Duplication

Lines 20
Ratio 19.23 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 20
loc 104
rs 2
cc 24
eloc 61
nc 26402
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
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 8 and the first side effect is on line 5.

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
namespace Fluentd\Log;
4
5
class File extends \Fluentd\Log
0 ignored issues
show
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
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
			for(;$i<count($backtrace);$i++){
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);
76
						}
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '}'
Loading history...
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));
107
			umask($old);
108
		}
109
110
		return true;
111
	}
112
}
113