Completed
Branch master (435868)
by Thiago Augustus de
03:42 queued 01:41
created

NoXMLSpace.class.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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) {
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 );
47
// Example: var_dump( '<pre>', NoXMLSpace::noSpace($xml) );
48