Completed
Push — master ( 1c2760...0b9462 )
by mw
69:58 queued 34:59
created

SMWStringValue::getWikiValueByLengthOf()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
use SMW\DataValues\ValueFormatters\DataValueFormatter;
4
use SMWDataItem as DataItem;
5
use SMWDataValue as DataValue;
6
use SMWDIBlob as DIBlob;
7
8
/**
9
 * This datavalue implements String-Datavalues suitable for defining
10
 * String-types of properties.
11
 *
12
 * @license GNU GPL v2+
13
 * @since 1.6
14
 *
15
 * @author Nikolas Iwan
16
 * @author Markus Krötzsch
17
 */
18
class SMWStringValue extends DataValue {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
19
20
	/**
21
	 * @see DataValue::parseUserValue
22
	 *
23
	 * @param string $value
24
	 */
25 85
	protected function parseUserValue( $value ) {
26
27 85
		if ( $value === '' ) {
28 1
			$this->addErrorMsg( 'smw_emptystring' );
29
		}
30
31 85
		$this->m_dataitem = new DIBlob( $value );
32 85
	}
33
34
	/**
35
	 * @see DataValue::loadDataItem
36
	 *
37
	 * @param SMWDataItem $dataitem
0 ignored issues
show
Documentation introduced by
There is no parameter named $dataitem. Did you maybe mean $dataItem?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
38
	 *
39
	 * @return boolean
40
	 */
41 80
	protected function loadDataItem( DataItem $dataItem ) {
42
43 80
		if ( !$dataItem instanceof DIBlob ) {
44
			return false;
45
		}
46
47 80
		$this->m_caption = false;
0 ignored issues
show
Documentation Bug introduced by
The property $m_caption was declared of type string, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
48 80
		$this->m_dataitem = $dataItem;
49
50 80
		return true;
51
	}
52
53
	/**
54
	 * @see DataValue::getShortWikiText
55
	 *
56
	 * @return string
57
	 */
58 73
	public function getShortWikiText( $linker = null ) {
59 73
		return $this->getDataValueFormatter()->format( DataValueFormatter::WIKI_SHORT, $linker );
60
	}
61
62
	/**
63
	 * @see DataValue::getShortHTMLText
64
	 *
65
	 * @return string
66
	 */
67 1
	public function getShortHTMLText( $linker = null ) {
68 1
		return $this->getDataValueFormatter()->format( DataValueFormatter::HTML_SHORT, $linker );
69
	}
70
71
	/**
72
	 * @see DataValue::getLongWikiText
73
	 *
74
	 * @return string
75
	 */
76
	public function getLongWikiText( $linker = null ) {
77
		return $this->getDataValueFormatter()->format( DataValueFormatter::WIKI_LONG, $linker );
78
	}
79
80
	/**
81
	 * @todo Rather parse input to obtain properly formatted HTML.
82
	 * @see DataValue::getLongHTMLText
83
	 *
84
	 * @return string
85
	 */
86 2
	public function getLongHTMLText( $linker = null ) {
87 2
		return $this->getDataValueFormatter()->format( DataValueFormatter::HTML_LONG, $linker );
88
	}
89
90
	/**
91
	 * @see DataValue::getWikiValue
92
	 *
93
	 * @return string
94
	 */
95 68
	public function getWikiValue() {
96 68
		return $this->getDataValueFormatter()->format( DataValueFormatter::VALUE );
97
	}
98
99 2
	public function getWikiValueByLengthOf( $length ) {
100
101 2
		if ( mb_strlen( $this->getWikiValue() ) > $length ) {
102 1
			return mb_substr( $this->getWikiValue(), 0, $length );
103
		}
104
105 2
		return $this->getWikiValue();
106
	}
107
108 1
	public function getInfolinks() {
109
110 1
		if ( $this->m_typeid != '_cod' ) {
111 1
			return parent::getInfolinks();
112
		}
113
114
		return array();
115
	}
116
117 1
	protected function getServiceLinkParams() {
118
119 1
		if ( !$this->isValid() ) {
120
			return false;
121
		}
122
123
		// Create links to mapping services based on a wiki-editable message. The parameters
124
		// available to the message are:
125
		// $1: urlencoded string
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% 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...
126 1
		return array( rawurlencode( $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...
Bug Best Practice introduced by
The return type of return array(rawurlencod...ataitem->getString())); (string[]) is incompatible with the return type of the parent method SMWDataValue::getServiceLinkParams of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
127
	}
128
129
}
130