Completed
Push — master ( 176090...b969a1 )
by Peter
02:10
created

Checker::isValid()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 10.0328

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 27
cts 29
cp 0.931
rs 7.1151
c 0
b 0
f 0
cc 10
nc 8
nop 1
crap 10.0328

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
2
3
4
namespace Maslosoft\Addendum\Cache\PhpCache;
5
6
7
use function basename;
8
use DirectoryIterator;
9
use function file_exists;
10
use function filemtime;
11
use function is_dir;
12
use Maslosoft\Addendum\Utilities\NameNormalizer;
13
use ReflectionClass;
14
use function str_replace;
15
16
class Checker extends CacheComponent
17
{
18 3
	public function isValid($className)
19
	{
20 3
		NameNormalizer::normalize($className, false);
21 3
		$info = new ReflectionClass($className);
22 3
		$filename = $info->getFileName();
23 3
		$cacheFilename = $this->getFilename($className);
24 3
		$fileTime = filemtime($filename);
25
26 3
		$cacheTime = 0;
27 3
		if(file_exists($cacheFilename))
28
		{
29 2
			$cacheTime = filemtime($cacheFilename);
30
		}
31 3
		if($fileTime > $cacheTime)
32
		{
33 2
			return false;
34
		}
35
36 1
		$partialsDir = $this->getPartialsDir($className);
37
38 1
		if(file_exists($partialsDir) && is_dir($partialsDir))
39
		{
40 1
			NameNormalizer::normalize($className, false);
41 1
			foreach (new DirectoryIterator($partialsDir) as $info)
42
			{
43 1
				$usingFile = $info->getFilename();
44 1
				if (strpos($usingFile, '.php') === false)
45
				{
46 1
					continue;
47
				}
48 1
				$usingClass = basename($usingFile, '.php');
49 1
				$usingClass = str_replace('.', '\\', $usingClass);
50 1
				NameNormalizer::normalize($usingClass, false);
51
52 1
				if (empty($usingClass))
53
				{
54
					continue;
55
				}
56
				// Prevent infinite loops
57 1
				if ($usingClass === $className)
58
				{
59
					continue;
60
				}
61 1
				$usingTime = filemtime((new ReflectionClass($usingClass))->getFileName());
62
63
				// Compares partial file time with cache time
64 1
				if ($usingTime > $cacheTime)
65
				{
66 1
					return false;
67
				}
68
69
			}
70
		}
71 1
		return true;
72
	}
73
}