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\DataValues\ValueFormatters; |
||
4 | |||
5 | use RuntimeException; |
||
6 | use SMW\Highlighter; |
||
7 | use SMWDataValue as DataValue; |
||
8 | use SMWNumberValue as NumberValue; |
||
9 | |||
10 | /** |
||
11 | * @license GNU GPL v2+ |
||
12 | * @since 2.4 |
||
13 | * |
||
14 | * @author mwjames |
||
15 | */ |
||
16 | class NumberValueFormatter extends DataValueFormatter { |
||
17 | |||
18 | /** |
||
19 | * @since 2.4 |
||
20 | * |
||
21 | * {@inheritDoc} |
||
22 | */ |
||
23 | 82 | public function isFormatterFor( DataValue $dataValue ) { |
|
24 | 82 | return $dataValue instanceOf NumberValue; |
|
25 | } |
||
26 | |||
27 | /** |
||
28 | * @since 2.4 |
||
29 | * |
||
30 | * {@inheritDoc} |
||
31 | */ |
||
32 | 52 | public function format( $type, $linker = null ) { |
|
33 | |||
34 | 52 | if ( !$this->dataValue instanceOf NumberValue ) { |
|
35 | 1 | throw new RuntimeException( "The formatter is missing a valid NumberValue object" ); |
|
36 | } |
||
37 | |||
38 | 51 | if ( $type === self::VALUE ) { |
|
39 | 16 | return $this->doFormatByValue(); |
|
40 | } |
||
41 | |||
42 | 43 | if ( $type === self::WIKI_SHORT || $type === self::HTML_SHORT ) { |
|
43 | 37 | return $this->doFormatByShort( $linker ); |
|
44 | } |
||
45 | |||
46 | 8 | if ( $type === self::WIKI_LONG || $type === self::HTML_LONG ) { |
|
47 | 7 | return $this->doFormatByLong( $linker ); |
|
48 | } |
||
49 | |||
50 | 1 | return 'UNKNOWN'; |
|
51 | } |
||
52 | |||
53 | 16 | private function doFormatByValue() { |
|
54 | |||
55 | 16 | if ( !$this->dataValue->isValid() ) { |
|
56 | 1 | return 'error'; |
|
57 | } |
||
58 | |||
59 | 15 | $unit = $this->dataValue->getUnit(); |
|
60 | |||
61 | 15 | $number = $this->dataValue->getNormalizedFormattedNumber( |
|
62 | 15 | $this->dataValue->getNumber() |
|
63 | ); |
||
64 | |||
65 | 15 | if ( $unit === '' ) { |
|
66 | 11 | return $number; |
|
67 | } |
||
68 | |||
69 | 6 | return $this->dataValue->hasPrefixalUnitPreference( $unit ) ? $unit . ' ' . $number : $number . ' ' . $unit; |
|
70 | } |
||
71 | |||
72 | 37 | private function doFormatByShort( $linker = null ) { |
|
73 | |||
74 | 37 | $outformat = $this->dataValue->getOutputFormat(); |
|
75 | |||
76 | 37 | if ( $linker === null || ( $linker === false ) || ( $outformat == '-' ) || ( $outformat == '-u' ) || ( $outformat == '-n' ) || !$this->dataValue->isValid() ) { |
|
77 | 11 | return $this->dataValue->getCaption(); |
|
78 | } |
||
79 | |||
80 | 31 | $convertedUnitValues = $this->dataValue->getConvertedUnitValues(); |
|
81 | 31 | $tooltip = ''; |
|
82 | |||
83 | 31 | $i = 0; |
|
84 | 31 | $sep = ''; |
|
0 ignored issues
–
show
|
|||
85 | |||
86 | 31 | foreach ( $convertedUnitValues as $unit => $value ) { |
|
87 | 31 | if ( $unit != $this->dataValue->getCanonicalMainUnit() ) { |
|
88 | 16 | $number = $this->dataValue->getLocalizedFormattedNumber( $value ); |
|
89 | 16 | if ( $unit !== '' ) { |
|
90 | 16 | $tooltip .= $this->dataValue->hasPrefixalUnitPreference( $unit ) ? $unit . ' ' . $number : $number . ' ' . $unit; |
|
91 | } else{ |
||
92 | $tooltip .= $number; |
||
93 | } |
||
94 | 16 | $tooltip .= ' <br />'; |
|
95 | 16 | $i++; |
|
96 | 16 | if ( $i >= 5 ) { // limit number of printouts in tooltip |
|
97 | 31 | break; |
|
98 | } |
||
99 | } |
||
100 | } |
||
101 | |||
102 | 31 | if ( $tooltip === '' ) { |
|
103 | 23 | return $this->dataValue->getCaption(); |
|
104 | } |
||
105 | |||
106 | 16 | $highlighter = Highlighter::factory( Highlighter::TYPE_QUANTITY, $this->dataValue->getOptionValueFor( 'user.language' ) ); |
|
107 | 16 | $highlighter->setContent( array ( |
|
108 | 16 | 'caption' => $this->dataValue->getCaption(), |
|
109 | 16 | 'content' => $tooltip |
|
110 | ) ); |
||
111 | |||
112 | 16 | return $highlighter->getHtml(); |
|
113 | } |
||
114 | |||
115 | 7 | private function doFormatByLong( $linker = null ) { |
|
116 | |||
117 | 7 | if ( !$this->dataValue->isValid() ) { |
|
118 | return $this->dataValue->getErrorText(); |
||
119 | } |
||
120 | |||
121 | 7 | $outformat = $this->dataValue->getOutputFormat(); |
|
122 | 7 | $convertedUnitValues = $this->dataValue->getConvertedUnitValues(); |
|
123 | |||
124 | 7 | $result = ''; |
|
125 | 7 | $i = 0; |
|
126 | |||
127 | 7 | foreach ( $convertedUnitValues as $unit => $value ) { |
|
128 | |||
129 | 7 | if ( $i == 1 ) { |
|
130 | 3 | $result .= ' ('; |
|
131 | 7 | } elseif ( $i > 1 ) { |
|
132 | 2 | $result .= ', '; |
|
133 | } |
||
134 | |||
135 | 7 | $number = ( $outformat != '-' ? $this->dataValue->getLocalizedFormattedNumber( $value ) : $value ); |
|
136 | |||
137 | 7 | if ( $unit !== '' ) { |
|
138 | 3 | $result .= $this->dataValue->hasPrefixalUnitPreference( $unit ) ? $unit . ' ' . $number : $number . ' ' . $unit; |
|
139 | } else { |
||
140 | 5 | $result .= $number; |
|
141 | } |
||
142 | |||
143 | 7 | $i++; |
|
144 | |||
145 | 7 | if ( $outformat == '-' ) { // no further conversions for plain output format |
|
146 | 7 | break; |
|
147 | } |
||
148 | } |
||
149 | |||
150 | 7 | if ( $i > 1 ) { |
|
151 | 3 | $result .= ')'; |
|
152 | } |
||
153 | |||
154 | 7 | return $result; |
|
155 | } |
||
156 | |||
157 | } |
||
158 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.