Completed
Push — master ( a7894a...d2af4c )
by Thiago Augustus de
02:08
created

NoXMLSpace   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 20 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 7
loc 35
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B noSpace() 7 14 5

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
3
/**
4
 * NoXMLSpace
5
 * 
6
 * @author Thiago Augustus de Oliveira
7
 * @version 1.0.0
8
 * @copyright MIT Copyright (c) 2015
9
 */
10
final class NoXMLSpace
11
{
12
13
	/**
14
	 * noSpace
15
	 * Remove all spaces in both side of the words in a .xml file
16
	 *
17
	 * Here is an inline example:
18
	 * <code>
19
	 *   <?php
20
	 *     $file = simplexml_load_file($_FILES['xml']['tmp_name']);
21
	 *     $xml  = noSpace($xml);
22
	 *   ?>
23
	 * </code>
24
	 *
25
	 * @param xml
26
	 * @return xml
27
	 * @access public
28
	 */
29
	public static function noSpace( $xml = null )
30
	{
31
32
		if ( empty( $xml ) || is_null( $xml ) ) return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by NoXMLSpace::noSpace of type xml.

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...
33
34 View Code Duplication
		foreach ( $xml as $key => $value ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
			if ( sizeof( $value ) > 1 ) {
36
				noSpace( $value ); // Get next node
37
			} else {
38
				$xml->$key = trim( $value );
39
			}
40
		}
41
		return $xml;
42
	}
43
44
}
45
46
// $xml = simplexml_load_file( $xml_file );
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
47
// Example: var_dump( '<pre>', NoXMLSpace::noSpace($xml) );
48