Completed
Push — master ( 314506...335380 )
by mw
100:54 queued 62:54
created

includes/parserhooks/DeclareParserFunction.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SMW;
4
5
use Parser;
6
use PPFrame;
7
use SMWPropertyValue as PropertyValue;
8
9
/**
10
 * Class that provides the {{#declare}} parser function
11
 *
12
 * @see http://semantic-mediawiki.org/wiki/Help:Argument_declaration_in_templates
13
 *
14
 * @license GNU GPL v2+
15
 * @since   1.5.3
16
 *
17
 * @author Markus Krötzsch
18
 * @author Jeroen De Dauw
19
 */
20
class DeclareParserFunction {
21
22
	/**
23
	 * @var ParserData
24
	 */
25
	private $parserData;
26
27
	/**
28
	 * @var DIWikiPage
29
	 */
30
	private $subject;
31
32
	/**
33
	 * @since 2.1
34
	 *
35
	 * @param ParserData $parserData
36
	 */
37 4
	public function __construct( ParserData $parserData ) {
38 4
		$this->parserData = $parserData;
39 4
	}
40
41
	/**
42
	 * @since 2.1
43
	 *
44
	 * @param PPFrame $frame
45
	 * @param array $args
46
	 */
47 3
	public function parse( PPFrame $frame, array $args ) {
48
49
		// @todo Save as metadata
50 3
		if ( !$frame->isTemplate() ) {
51 2
			return '';
52
		}
53
54 1
		$this->subject = $this->parserData->getSemanticData()->getSubject();
55
56 1
		foreach ( $args as $arg ) {
57 1
			if ( trim( $arg ) !== '' ) {
58 1
				$expanded = trim( $frame->expand( $arg ) );
59 1
				$parts = explode( '=', $expanded, 2 );
60
61 1
				if ( count( $parts ) == 1 ) {
62
					$propertystring = $expanded;
63
					$argumentname = $expanded;
64
				} else {
65 1
					$propertystring = $parts[0];
66 1
					$argumentname = $parts[1];
67
				}
68
69 1
				$propertyValue = PropertyValue::makeUserProperty( $propertystring );
70 1
				$argument = $frame->getArgument( $argumentname );
71 1
				$valuestring = $frame->expand( $argument );
72
73 1
				if ( $propertyValue->isValid() ) {
74 1
					$this->matchValueArgument( $propertyValue, $propertystring, $valuestring );
75
				}
76
			}
77
		}
78
79 1
		$this->parserData->pushSemanticDataToParserOutput();
80
81 1
		return '';
82
	}
83
84 1
	private function matchValueArgument( PropertyValue $propertyValue, $propertystring, $valuestring ) {
85
86 1
		if ( $propertyValue->getPropertyTypeID() === '_wpg' ) {
87 1
			$matches = array();
88 1
			preg_match_all( '/\[\[([^\[\]]*)\]\]/u', $valuestring, $matches );
89 1
			$objects = $matches[1];
90
91 1
			if ( count( $objects ) == 0 ) {
92 1
				if ( trim( $valuestring ) !== '' ) {
93 1
					$this->addDataValue( $propertystring, $valuestring );
94
				}
95
			} else {
96
				foreach ( $objects as $object ) {
97 1
					$this->addDataValue( $propertystring, $object );
98
				}
99
			}
100
		} elseif ( trim( $valuestring ) !== '' ) {
101
			$this->addDataValue( $propertystring, $valuestring );
102
		}
103
104
		// $value = \SMW\DataValueFactory::getInstance()->newDataValueByProperty( $property->getDataItem(), $valuestring );
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
105
		// if (!$value->isValid()) continue;
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
106 1
	}
107
108 1
	private function addDataValue( $property, $value ) {
109
110 1
		$dataValue = DataValueFactory::getInstance()->newDataValueByText(
111
			$property,
112
			$value,
113 1
			false,
114 1
			$this->subject
115
		);
116
117 1
		$this->parserData->addDataValue( $dataValue );
118 1
	}
119
120
}
121