Completed
Push — master ( 59c431...219a45 )
by mw
36:38
created

Serializer::doSerializeProp()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 42
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 20
nop 2
dl 0
loc 42
ccs 19
cts 19
cp 1
crap 7
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Query\PrintRequest;
4
5
use SMW\Query\PrintRequest;
6
use SMW\Localizer;
7
8
/**
9
 * @license GNU GPL v2+
10
 * @since 2.5
11
 *
12
 * @author Markus Krötzsch
13
 * @author mwjames
14
 */
15
class Serializer {
16
17
	/**
18
	 * @since 2.5
19
	 *
20
	 * @param PrintRequest $printRequest
21
	 * @param boolean $showparams that sets if the serialization should include
22
	 * the extra print request parameters
23
	 *
24
	 * @return string
25
	 */
26 102
	public static function serialize( PrintRequest $printRequest, $showparams = false ) {
27 102
		$parameters = '';
28
29 102
		if ( $showparams ) {
30 9
			foreach ( $printRequest->getParameters() as $key => $value ) {
31 1
				$parameters .= "|+" . $key . "=" . $value;
32
			}
33
		}
34
35 102
		switch ( $printRequest->getMode() ) {
36 102
			case PrintRequest::PRINT_CATS:
37 4
				return self::doSerializeCat( $printRequest, $parameters );
38 101
			case PrintRequest::PRINT_CCAT:
39 1
				return self::doSerializeCcat( $printRequest, $parameters );
40 100
			case PrintRequest::PRINT_CHAIN:
41 100
			case PrintRequest::PRINT_PROP:
42 78
				return self::doSerializeProp( $printRequest, $parameters );
43 76
			case PrintRequest::PRINT_THIS:
44 76
				return self::doSerializeThis( $printRequest, $parameters );
45
			default:
46
				return '';
47
		}
48
49
		return ''; // no current serialisation
0 ignored issues
show
Unused Code introduced by
return ''; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
50
	}
51
52 4
	private static function doSerializeCat( $printRequest, $parameters ) {
53
54 4
		$catlabel = Localizer::getInstance()->getNamespaceTextById( NS_CATEGORY );
55 4
		$result = '?' . $catlabel;
56
57 4
		if ( $printRequest->getLabel() != $catlabel ) {
58 1
			$result .= '=' . $printRequest->getLabel();
59
		}
60
61 4
		return $result . $parameters;
62
	}
63
64 1
	private static function doSerializeCcat( $printRequest, $parameters ) {
65
66 1
		$printname = $printRequest->getData()->getPrefixedText();
67 1
		$result = '?' . $printname;
68
69 1
		if ( $printRequest->getOutputFormat() != 'x' ) {
70
			$result .= '#' . $printRequest->getOutputFormat();
71
		}
72
73 1
		if ( $printRequest->getLabel() != $printname ) {
74 1
			$result .= '=' . $printRequest->getLabel();
75
		}
76
77 1
		return $result . $parameters;
78
	}
79
80 78
	private static function doSerializeProp( $printRequest, $parameters ) {
81
82 78
		$printname = '';
83
84 78
		$label = $printRequest->getLabel();
85
		$data = $printRequest->getData();
86
87
		if ( $data->isVisible() ) {
88
			// #1564
89 76
			// Use the canonical form for predefined properties to ensure
90 2
			// that local representations are for display but points to
91
			// the correct property
92 75
			if ( $printRequest->isMode( PrintRequest::PRINT_CHAIN ) ) {
93
				$printname = $data->getDataItem()->getString();
94
				// If the preferred label and invoked label are the same
95
				// then no additional label is required as the label is
96 78
				// recognized as being available by the system
97
				if ( $label === $data->getLastPropertyChainValue()->getDataItem()->getPreferredLabel() ) {
98 78
					$label = $printname;
99 39
				}
100
			} else {
101
102 78
				$printname = $printRequest->getData()->getDataItem()->getCanonicalLabel();
103 43
104
				if ( $label === $data->getDataItem()->getPreferredLabel() ) {
105
					$label = $printname;
106 78
				}
107
			}
108
		}
109 76
110
		$result = '?' . $printname;
111 76
112
		if ( $printRequest->getOutputFormat() !== '' ) {
113 76
			$result .= '#' . $printRequest->getOutputFormat();
114 3
		}
115
116
		if ( $printname != $label ) {
117 76
			$result .= '=' . $label;
118 76
		}
119
120
		return $result . $parameters;
121 76
	}
122
123
	private static function doSerializeThis( $printRequest, $parameters ) {
124
125
		$result = '?';
126
127
		if ( $printRequest->getLabel() !== '' ) {
128
			$result .= '=' . $printRequest->getLabel();
129
		}
130
131
		if ( $printRequest->getOutputFormat() !== '' ) {
132
			$result .= '#' . $printRequest->getOutputFormat();
133
		}
134
135
		return $result . $parameters;
136
	}
137
138
}
139