Passed
Push — master ( 6fb715...de9f22 )
by Gabriel
27:49
created

FeatureToggleParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 5
eloc 16
dl 0
loc 32
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFeatureToggleChecks() 0 9 3
A parseMethodCalls() 0 9 2
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
	/**
36
	 * @param string $choiceFactoryLocation
37
	 * @return MethodCall[]
38
	 */
39 1
	private static function parseMethodCalls( string $choiceFactoryLocation ): array {
40 1
		$parser = ( new ParserFactory() )->create( ParserFactory::PREFER_PHP7 );
41 1
		$nodeFinder = new NodeFinder();
42 1
		$choiceFactoryCode = ( new SimpleFileFetcher() )->fetchFile( $choiceFactoryLocation );
43 1
		$syntaxTree = $parser->parse( $choiceFactoryCode );
44 1
		return $nodeFinder->find(
45 1
			$syntaxTree,
46
			function ( Node $node ) {
47 1
				return $node instanceof MethodCall && $node->name->toString() === self::FEATURE_TOGGLE_METHOD_NAME;
48 1
			}
49
		);
50
	}
51
}