Completed
Push — master ( 74101c...17956e )
by Peter
11:10
created

YamlString   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromModel() 0 4 1
A toModel() 0 4 1
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Mangan\Transformers;
10
11
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
12
use Maslosoft\Mangan\Exceptions\TransformatorException;
13
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
14
use Symfony\Component\Yaml\Yaml;
15
16
/**
17
 * YamlString
18
 *
19
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
20
 */
21
class YamlString implements TransformatorInterface
22
{
23
24
	/**
25
	 * Returns the given object as an associative array
26
	 * @param AnnotatedInterface|object $model
27
	 * @param string[] $fields Fields to transform
28
	 * @return array an associative array of the contents of this object
29
	 */
30
	public static function fromModel(AnnotatedInterface $model, $fields = [])
31
	{
32
		return Yaml::dump(YamlArray::fromModel($model, $fields));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Symfony\Componen...odel($model, $fields)); (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...
33
	}
34
35
	/**
36
	 * Create document from array
37
	 *
38
	 * @param mixed[] $data
39
	 * @param string|object $className
40
	 * @param AnnotatedInterface $instance
41
	 * @return AnnotatedInterface
42
	 * @throws TransformatorException
43
	 */
44
	public static function toModel($data, $className = null, AnnotatedInterface $instance = null)
45
	{
46
		return YamlArray::toModel(Yaml::parse($data), $className, $instance);
0 ignored issues
show
Documentation introduced by
$data is of type array<integer,*>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
	}
48
49
}
50