Completed
Pull Request — master (#38)
by mw
04:34
created

ResourceIdentifierStringValue::getTargetLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SCI\DataValues;
4
5
use SMWStringValue as StringValue;
6
use SMWDIBlob as DIBlob;
7
use Html;
8
9
/**
10
 * @license GNU GPL v2+
11
 * @since 1.0
12
 *
13
 * @author mwjames
14
 */
15
class ResourceIdentifierStringValue extends StringValue {
16
17
	/**
18
	 * @var ResourceIdentifierStringValueParser
19
	 */
20
	private $resourceIdentifierStringValueParser;
21
22
	/**
23
	 * @param string $typeid
24
	 */
25 42
	public function __construct( $typeid = '' ) {
26 42
		parent::__construct( $typeid );
27 42
		$this->resourceIdentifierStringValueParser = new ResourceIdentifierStringValueParser( $typeid );
28 42
	}
29
30
	/**
31
	 * @see StringValue::parseUserValue
32
	 */
33 36
	protected function parseUserValue( $value ) {
34
35 36
		$inputValue = $value;
36
	//	$this->m_caption = $value;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% 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...
37
38 36
		if ( !$this->resourceIdentifierStringValueParser->parse( $value ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->resourceIdentifie...ueParser->parse($value) of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
39 13
			$this->addError( wfMessage( 'sci-datavalue-invalid-id-value', $inputValue, $this->resourceIdentifierStringValueParser->getCanonicalName() )->inContentLanguage()->escaped() );
40 13
			$this->m_dataitem = new DIBlob( 'ERROR' );
41 13
			return;
42
		}
43
44 24
		parent::parseUserValue( $value );
45 24
	}
46
47
	/**
48
	 * @see StringValue::getShortWikiText
49
	 */
50 30
	public function getShortWikiText( $linker = null ) {
51
52 30
		if ( !$this->isValid() ) {
53 12
			return '';
54
		}
55
56 18
		if ( !$this->m_caption ) {
57 18
			$this->m_caption = $this->m_dataitem->getString();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataItem as the method getString() does only exist in the following sub-classes of SMWDataItem: SMWDIBlob, SMWDIError, SMWDIString. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
58 18
		}
59
60 18
		if ( $preferredCaption = $this->getPreferredCaption() ) {
61 1
			return $preferredCaption;
62
		}
63
64 18
		if ( $linker === null ) {
65 10
			return $this->m_caption;
66
		}
67
68 8
		return Html::rawElement(
69 8
			'span',
70
			array(
71
				'class' => 'plainlinks'
72 8
			),
73 8
			'[' . $this->getTargetLink( urlencode( $this->m_caption ) ) . ' ' . $this->m_caption .']'
74 8
		);
75
	}
76
77
	/**
78
	 * @see StringValue::getShortHTMLText
79
	 */
80 29
	public function getShortHTMLText( $linker = null ) {
81
82 29
		if ( !$this->isValid() ) {
83 12
			return '';
84
		}
85
86 17
		if ( !$this->m_caption ) {
87
			$this->m_caption = $this->m_dataitem->getString();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataItem as the method getString() does only exist in the following sub-classes of SMWDataItem: SMWDIBlob, SMWDIError, SMWDIString. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
88
		}
89
90 17
		if ( $preferredCaption = $this->getPreferredCaption() ) {
91
			return $preferredCaption;
92
		}
93
94 17
		if ( $linker === null ) {
95 10
			return $this->m_caption;
96
		}
97
98 7
		return Html::rawElement(
99 7
			'a',
100
			array(
101 7
				'href'   => $this->getTargetLink( $this->m_caption ),
102
				'target' => '_blank'
103 7
			),
104 7
			$this->m_caption
105 7
		);
106
	}
107
108
	/**
109
	 * @see StringValue::getLongWikiText
110
	 */
111 29
	public function getLongWikiText( $linked = null ) {
112 29
		return $this->getShortWikiText( $linked );
113
	}
114
115
	/**
116
	 * @see StringValue::getLongHTMLText
117
	 */
118 29
	public function getLongHTMLText( $linker = null ) {
119 29
		return $this->getShortHTMLText( $linker );
120
	}
121
122
	/**
123
	 * @see DataValue::getPreferredCaption
124
	 */
125 18
	public function getPreferredCaption() {
126
127 18
		if ( $this->m_outformat == '-' ) {
128 1
			return $this->m_caption;
129
		}
130
131 18
		return false;
132
	}
133
134 8
	private function getTargetLink( $target ) {
135 8
		return $this->resourceIdentifierStringValueParser->getResourceTargetUri() . $target;
136
	}
137
138
}
139