Completed
Push — master ( 0e16bb...b7cc2d )
by Peter
29:55 queued 26:31
created

JsonString   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 2
c 2
b 1
f 0
lcom 0
cbo 1
dl 0
loc 30
ccs 4
cts 4
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromModel() 0 4 1
A toModel() 0 4 1
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link http://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Transformers;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Exceptions\TransformatorException;
18
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
19
20
/**
21
 * JsonString
22
 *
23
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
24
 */
25
class JsonString implements TransformatorInterface
26
{
27
28
	/**
29
	 * Returns the given object as an associative array
30
	 * @param AnnotatedInterface|object $model
31
	 * @param string[] $fields Fields to transform
32
	 * @param int $options json_encode options
33
	 * @return array an associative array of the contents of this object
34
	 */
35 2
	public static function fromModel(AnnotatedInterface $model, $fields = [], $options = null)
36
	{
37 2
		return json_encode(JsonArray::fromModel($model, $fields), $options);
1 ignored issue
show
Bug Best Practice introduced by
The return type of return json_encode(\Masl...l, $fields), $options); (string) is incompatible with the return type declared by the interface Maslosoft\Mangan\Interfa...torInterface::fromModel of type array.

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...
38
	}
39
40
	/**
41
	 * Create document from array
42
	 *
43
	 * @param mixed[] $data
44
	 * @param string|object $className
45
	 * @param AnnotatedInterface $instance
46
	 * @return AnnotatedInterface
47
	 * @throws TransformatorException
48
	 */
49 2
	public static function toModel($data, $className = null, AnnotatedInterface $instance = null)
50
	{
51 2
		return JsonArray::toModel(json_decode($data, true), $className, $instance);
52
	}
53
54
}
55