Completed
Push — master ( a38f02...4631a3 )
by Peter
02:48
created

CoarseChecker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 45
ccs 10
cts 12
cp 0.8333
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B mightHaveAnnotations() 0 31 6
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Addendum\Helpers;
10
11
use ReflectionClass;
12
use ReflectionObject;
13
use Reflector;
14
15
/**
16
 * Coarse check for annotations.
17
 *
18
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
19
 */
20
class CoarseChecker
21
{
22
23
	/**
24
	 * Check if file contains annotations,
25
	 * by checking if it contains @[A-Z] regular expression.
26
	 *
27
	 * It does not ensure that file really contains anniotations.
28
	 *
29
	 * @param string|Reflector|object $entity
30
	 * @return bool
31
	 */
32 46
	public static function mightHaveAnnotations($entity)
33
	{
34 46
		if (is_object($entity))
35
		{
36 46
			if ($entity instanceof Reflector)
37
			{
38 46
				if ($entity instanceof ReflectionClass)
39
				{
40 37
					$file = $entity->getFileName();
41
				}
42
				else
43
				{
44 46
					$file = $entity->getDeclaringClass()->getFileName();
45
				}
46
			}
47
			else
48
			{
49 46
				$file = (new ReflectionObject($entity))->getFileName();
50
			}
51
		}
52
		else
53
		{
54
			$file = $entity;
55
		}
56 46
		if (empty($file) || !is_string($file))
57
		{
58
			return false;
59
		}
60 46
		$content = file_get_contents($file);
61 46
		return !!preg_match('~@[A-Z]~', $content);
62
	}
63
64
}
65