Completed
Push — dev ( 3bce75...1a2bfc )
by Auke
09:55
created

ar_urlTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 118
Duplicated Lines 15.25 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 0
cbo 3
dl 18
loc 118
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testparseUrl() 0 19 1
A testparseUrlQueryMultipleElements() 0 9 1
A testparseUrlQueryUnnumberedElements() 9 9 1
A testparseUrlQueryNumberedElements() 9 9 1
A testModQuery() 0 10 1
A testParseAuthority() 0 7 1
A testCopyQuery() 0 9 1
A testCopyAndModQuery() 0 14 1
A testParseCommonURLS() 0 20 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
class ar_urlTest extends AriadneBaseTest {
3
4
	function testparseUrl()
5
	{
6
		$starturl = 'http://www.ariadne-cms.org/?frop=1';
7
		$url = ar::url( $starturl );
8
		$this->assertInstanceOf( 'ar_url', $url );
9
		$this->assertEquals( $starturl, ''.$url );
10
11
		$starturl = 'http://www.ariadne-cms.org/?frop=1&frml=2';
12
		$url = ar::url( $starturl );
13
		$url->fragment = 'test123';
0 ignored issues
show
Documentation introduced by
The property fragment does not exist on object<ar_url>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
14
		$this->assertEquals( $starturl .'#test123', ''.$url);
15
16
		$starturl = 'http://www.ariadne-cms.org/view.html?foo=some+thing';
17
		$url = ar::url( $starturl );
18
		$this->assertInstanceOf( 'ar_url', $url );
19
		$this->assertInstanceOf( 'ar_urlQuery', $url->query );
20
		$this->assertEquals( $starturl, ''.$url );
21
		$this->assertEquals( $url->query['foo'], 'some thing' );
22
	}
23
24
	function testparseUrlQueryMultipleElements()
25
	{
26
		$starturl = 'http://www.ariadne-cms.org/?test=test&test=frop';
27
		$url = ar::url( $starturl );
28
		$this->assertInstanceOf( 'ar_url', $url );
29
		$this->assertInstanceOf( 'ar_urlQuery', $url->query );
30
		$this->assertEquals( 'frop', ''.$url->query['test'], "PHP url parser, the second instance has precedence");
31
		$this->assertNotEquals( $starturl, ''.$url );
32
	}
33
34 View Code Duplication
	function testparseUrlQueryUnnumberedElements()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
	{
36
		$starturl = 'http://www.ariadne-cms.org/?test[]=test&test[]=frop';
37
		$url = ar::url( $starturl );
38
		$this->assertInstanceOf( 'ar_url', $url );
39
		$this->assertInstanceOf( 'ar_urlQuery', $url->query );
40
		$this->assertEquals( ['test', 'frop'], $url->query['test'], "Auto indexed array from query");
41
		$this->assertEquals( (string)$url, (string)ar::url($url) );
42
	}
43
44 View Code Duplication
	function testparseUrlQueryNumberedElements()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
	{
46
		$starturl = 'http://www.ariadne-cms.org/?test[1]=test&test[0]=frop';
47
		$url = ar::url( $starturl );
48
		$this->assertInstanceOf( 'ar_url', $url );
49
		$this->assertInstanceOf( 'ar_urlQuery', $url->query );
50
		$this->assertEquals( ['frop', 'test'], $url->query['test'], "manual index array from query");
51
		$this->assertEquals( (string)$url, (string)ar::url($url) );
52
	}
53
54
	function testModQuery()
55
	{
56
		$base = 'http://host/path/to?test=';
57
		$url = ar::url($base .'1');
58
		$url->query['test'] = "3";
59
		$this->assertEquals( $base .'3', (string)$url );
60
61
		$url->query['test'] = ['foo', 'bar'];
62
		$this->assertEquals( 'http://host/path/to?test%5B0%5D=foo&test%5B1%5D=bar', (string)$url );
63
	}
64
65
	function testParseAuthority()
66
	{
67
		$starturl = 'http://foo:[email protected]:80/';
68
		$url = ar::url( $starturl );
69
		$this->assertInstanceOf( 'ar_url', $url );
70
		$this->assertEquals( $starturl, $url.'' );
71
	}
72
73
	function testCopyQuery()
74
	{
75
		$url1 = ar::url('http://host/path/to?test=1');
76
		$url2 = ar::url('http://host/path/to?test=2');
77
		$this->assertNotEquals( $url1, $url2);
78
79
		$url2->query = $url1->query;
0 ignored issues
show
Documentation introduced by
The property $query is declared private in ar_url. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
80
		$this->assertEquals( $url1, $url2);
81
	}
82
83
	function testCopyAndModQuery()
84
	{
85
		$url1 = ar::url('http://host/path/to?test=1');
86
		$url2 = ar::url('http://host/path/to?test=2');
87
		$this->assertNotEquals( $url1, $url2);
88
89
		$url2->query = $url1->query;
0 ignored issues
show
Documentation introduced by
The property $query is declared private in ar_url. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
		$this->assertEquals( $url1, $url2);
91
92
		$url2->query['test'] = '3';
93
		$this->assertEquals( '3', $url2->query['test']);
94
		$this->assertEquals( '1', $url1->query['test']);
95
96
	}
97
98
99
	function testParseCommonURLS()
100
	{
101
		$commonUrls = [
102
			'ftp://ftp.is.co.za/rfc/rfc1808.txt',
103
			'http://www.ietf.org/rfc/rfc2396.txt',
104
			'ldap://[2001:db8::7]/c=GB?objectClass?one',
105
			'mailto:[email protected]',
106
			'news:comp.infosystems.www.servers.unix',
107
			'tel:+1-816-555-1212',
108
			'telnet://192.0.2.16:80/',
109
			'urn:oasis:names:specification:docbook:dtd:xml:4.1.2',
110
			'//google.com',
111
			'../../relative/',
112
			'file:///C:/'
113
		];
114
		foreach ($commonUrls as $sourceUrl) {
115
			$url = ar::url( $sourceUrl );
116
			$this->assertEquals( $sourceUrl, ''.$url );
117
		}
118
	}
119
}
120