SemanticMediaWiki /
SemanticExtraSpecialProperties
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 SESP\PropertyAnnotators; |
||
| 4 | |||
| 5 | use SMW\DIProperty; |
||
| 6 | use SMW\SemanticData; |
||
| 7 | use SMWDataItem as DataItem; |
||
| 8 | use SMWDIUri as DIUri; |
||
| 9 | use SESP\PropertyAnnotator; |
||
| 10 | use SESP\AppFactory; |
||
| 11 | use Title; |
||
| 12 | use RuntimeException; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @private |
||
| 16 | * @ingroup SESP |
||
| 17 | * |
||
| 18 | * @license GNU GPL v2+ |
||
| 19 | * @since 2.0 |
||
| 20 | * |
||
| 21 | * @author mwjames |
||
| 22 | * @author rotsee |
||
| 23 | */ |
||
| 24 | class ShortUrlPropertyAnnotator implements PropertyAnnotator { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Predefined property ID |
||
| 28 | */ |
||
| 29 | const PROP_ID = '___SHORTURL'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var AppFactory |
||
| 33 | */ |
||
| 34 | private $appFactory; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @since 2.0 |
||
| 38 | * |
||
| 39 | * @param AppFactory $appFactory |
||
| 40 | */ |
||
| 41 | 2 | public function __construct( AppFactory $appFactory ) { |
|
| 42 | 2 | $this->appFactory = $appFactory; |
|
| 43 | 2 | } |
|
| 44 | |||
| 45 | /** |
||
| 46 | * @since 2.0 |
||
| 47 | * |
||
| 48 | * {@inheritDoc} |
||
| 49 | */ |
||
| 50 | 1 | public function isAnnotatorFor( DIProperty $property ) { |
|
| 51 | 1 | return $property->getKey() === self::PROP_ID; |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @since 2.0 |
||
| 56 | * |
||
| 57 | * {@inheritDoc} |
||
| 58 | */ |
||
| 59 | 2 | public function addAnnotation( DIProperty $property, SemanticData $semanticData ) { |
|
| 60 | |||
| 61 | 2 | if ( !$this->hasShortUrlUtils() ) { |
|
| 62 | 1 | throw new RuntimeException( 'Class ShortUrlUtils is not available' ); |
|
| 63 | } |
||
| 64 | |||
| 65 | 1 | $dataItem = null; |
|
| 66 | 1 | $shortUrl = $this->getShortUrl( $semanticData->getSubject()->getTitle() ); |
|
|
0 ignored issues
–
show
|
|||
| 67 | |||
| 68 | 1 | if ( $shortUrl !== null ) { |
|
| 69 | 1 | $dataItem = new DIUri( 'http', $shortUrl, '', '' ); |
|
| 70 | 1 | } |
|
| 71 | |||
| 72 | 1 | if ( $dataItem instanceof DataItem ) { |
|
| 73 | 1 | $semanticData->addPropertyObjectValue( $property, $dataItem ); |
|
| 74 | 1 | } |
|
| 75 | 1 | } |
|
| 76 | |||
| 77 | protected function getShortUrl( Title $title ) { |
||
| 78 | |||
| 79 | //FIXME handle internal and external links |
||
| 80 | $shortUrl = null; |
||
| 81 | |||
| 82 | if ( \ShortUrlUtils::needsShortUrl( $title ) ) { |
||
| 83 | $shortUrl = $this->getUrlPrefix() . \ShortUrlUtils::encodeTitle( $title ); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $shortUrl; |
||
| 87 | } |
||
| 88 | |||
| 89 | protected function getUrlPrefix() { |
||
| 90 | |||
| 91 | $shortUrlPrefix = $this->appFactory->getOption( 'wgShortUrlPrefix', '' ); |
||
|
0 ignored issues
–
show
'' is of type string, but the function expects a boolean.
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 92 | |||
| 93 | if ( $shortUrlPrefix === '' ) { |
||
| 94 | return SpecialPage::getTitleFor( 'ShortUrl' )->getFullUrl() . '/'; |
||
| 95 | } |
||
| 96 | |||
| 97 | return $shortUrlPrefix; |
||
| 98 | } |
||
| 99 | |||
| 100 | protected function hasShortUrlUtils() { |
||
| 101 | return class_exists( 'ShortUrlUtils' ); |
||
| 102 | } |
||
| 103 | |||
| 104 | } |
||
| 105 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: