Passed
Push — master ( de9f22...184dfe )
by Jeroen De
02:28
created

FeatureToggleParser::getFeatureToggleChecks()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 9
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\BucketTesting\Validation;
6
7
use FileFetcher\SimpleFileFetcher;
8
use PhpParser\Node;
9
use PhpParser\Node\Expr\MethodCall;
10
use PhpParser\NodeFinder;
11
use PhpParser\ParserFactory;
12
13
/**
14
 * @license GNU GPL v2+
15
 */
16
class FeatureToggleParser {
17
18
	/** @see \WMDE\Fundraising\Frontend\BucketTesting\FeatureToggle::featureIsActive */
19
	const FEATURE_TOGGLE_METHOD_NAME = 'featureIsActive';
20
21
	/**
22
	 * @return string[]
23
	 */
24 1
	public static function getFeatureToggleChecks( string $choiceFactoryLocation ): array {
25 1
		$featureToggleChecks = [];
26 1
		foreach ( self::parseMethodCalls( $choiceFactoryLocation ) as $featureToggleCheck ) {
27 1
			if ( count( $featureToggleCheck->args ) !== 1 ) {
28
				throw new \LogicException( self::FEATURE_TOGGLE_METHOD_NAME . ' should have exactly one argument.' );
29
			}
30 1
			$featureToggleChecks[] = $featureToggleCheck->args[0]->value->value;
31
		}
32 1
		return $featureToggleChecks;
33
	}
34
35 1
	/**
36 1
	 * @param string $choiceFactoryLocation
37 1
	 * @return MethodCall[]
38 1
	 */
39 1
	private static function parseMethodCalls( string $choiceFactoryLocation ): array {
40 1
		$parser = ( new ParserFactory() )->create( ParserFactory::PREFER_PHP7 );
41 1
		$nodeFinder = new NodeFinder();
42
		$choiceFactoryCode = ( new SimpleFileFetcher() )->fetchFile( $choiceFactoryLocation );
43 1
		$syntaxTree = $parser->parse( $choiceFactoryCode );
44 1
		return $nodeFinder->find(
45
			$syntaxTree,
46
			function ( Node $node ) {
47
				return $node instanceof MethodCall && $node->name->toString() === self::FEATURE_TOGGLE_METHOD_NAME;
48
			}
49
		);
50
	}
51
}