YamlString::fromModel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 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 https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Transformers;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\AspectManager;
18
use Maslosoft\Mangan\Exceptions\TransformatorException;
19
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
20
use Symfony\Component\Yaml\Yaml;
21
22
/**
23
 * YamlString
24
 *
25
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
26
 */
27
class YamlString implements TransformatorInterface
28
{
29
	const AspectYamlStringFromModel = 'AspectYamlStringFromModel';
30
	const AspectYamlStringToModel = 'AspectYamlStringToModel';
31
32
	/**
33
	 * Returns the given object as an associative array
34
	 * @param AnnotatedInterface|object $model
35
	 * @param string[] $fields Fields to transform
36
	 *
37
	 * @param int   $inline                 The level where you switch to inline YAML
38
	 * @param int   $indent                 The amount of spaces to use for indentation of nested nodes.
39
	 * @param bool  $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
40
	 * @return string YAML string with the contents of this object
41
	 */
42 2
	public static function fromModel(AnnotatedInterface $model, $fields = [], $inline = 2, $indent = 4, $exceptionOnInvalidType = false)
43
	{
44 2
		AspectManager::addAspect($model, self::AspectYamlStringFromModel);
45 2
		$data = Yaml::dump(YamlArray::fromModel($model, $fields), $inline, $indent, $exceptionOnInvalidType);
0 ignored issues
show
Documentation introduced by
$exceptionOnInvalidType is of type boolean, but the function expects a integer.

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...
46 2
		AspectManager::removeAspect($model, self::AspectYamlStringFromModel);
47 2
		return $data;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $data; (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...
48
	}
49
50
	/**
51
	 * Create document from array
52
	 *
53
	 * @param mixed[] $data
54
	 * @param string|object $className
55
	 * @param AnnotatedInterface $instance
56
	 *
57
	 * @param bool   $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
58
	 *
59
	 * @return AnnotatedInterface
60
	 * @throws TransformatorException
61
	 */
62 2
	public static function toModel($data, $className = null, AnnotatedInterface $instance = null, $exceptionOnInvalidType = false)
63
	{
64 2
		AspectManager::addAspect($instance, self::AspectYamlStringToModel);
65 2
		$model = YamlArray::toModel(Yaml::parse($data, $exceptionOnInvalidType), $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...
Documentation introduced by
$exceptionOnInvalidType is of type boolean, but the function expects a integer.

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...
Bug introduced by
It seems like $className defined by parameter $className on line 62 can also be of type object; however, Maslosoft\Mangan\Transformers\YamlArray::toModel() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
66 2
		AspectManager::removeAspect($instance, self::AspectYamlStringToModel);
67 2
		AspectManager::removeAspect($model, self::AspectYamlStringToModel);
68 2
		return $model;
69
	}
70
71
}
72