Completed
Push — master ( b5b8be...5ff268 )
by Peter
13:31
created

CoarseChecker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 84.21%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 6
c 2
b 1
f 0
lcom 0
cbo 0
dl 0
loc 45
ccs 16
cts 19
cp 0.8421
rs 10

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 43
	public static function mightHaveAnnotations($entity)
33
	{
34 43
		if (is_object($entity))
35 43
		{
36 43
			if ($entity instanceof Reflector)
37 43
			{
38 43
				if ($entity instanceof ReflectionClass)
39 43
				{
40 34
					$file = $entity->getFileName();
41 34
				}
42
				else
43
				{
44 25
					$file = $entity->getDeclaringClass()->getFileName();
45
				}
46 43
			}
47
			else
48
			{
49
				$file = (new ReflectionObject($entity))->getFileName();
50
			}
51 43
		}
52
		else
53
		{
54
			$file = $entity;
55
		}
56 43
		if (empty($file) || !is_string($file))
57 43
		{
58
			return false;
59
		}
60 43
		$content = file_get_contents($file);
61 43
		return !!preg_match('~@[A-Z]~', $content);
62
	}
63
64
}
65