PrimitiveWrapper::wrap()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 10
cp 0
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 10
nc 5
nop 3
crap 42
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of the pinepain/js-sandbox PHP library.
5
 *
6
 * Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
7
 *
8
 * Licensed under the MIT license: http://opensource.org/licenses/MIT
9
 *
10
 * For the full copyright and license information, please view the
11
 * LICENSE file that was distributed with this source or visit
12
 * http://opensource.org/licenses/MIT
13
 */
14
15
16
namespace Pinepain\JsSandbox\Wrappers;
17
18
19
use V8\BooleanValue;
20
use V8\Context;
21
use V8\Isolate;
22
use V8\NullValue;
23
use V8\NumberValue;
24
use V8\StringValue;
25
use function is_bool;
26
use function is_float;
27
use function is_int;
28
use function is_null;
29
use function is_string;
30
31
32
class PrimitiveWrapper implements WrapperInterface
33
{
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function wrap(Isolate $isolate, Context $context, $value)
38
    {
39
        if (is_null($value)) {
40
            return new NullValue($isolate);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \V8\NullValue($isolate); (V8\NullValue) is incompatible with the return type declared by the interface Pinepain\JsSandbox\Wrappers\WrapperInterface::wrap of type V8\Value|V8\PrimitiveVal...bject|V8\FunctionObject.

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...
41
        }
42
43
        if (is_bool($value)) {
44
            return new BooleanValue($isolate, $value);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \V8\BooleanValue($isolate, $value); (V8\BooleanValue) is incompatible with the return type declared by the interface Pinepain\JsSandbox\Wrappers\WrapperInterface::wrap of type V8\Value|V8\PrimitiveVal...bject|V8\FunctionObject.

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...
45
        }
46
47
        if (is_string($value)) {
48
            return new StringValue($isolate, $value);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \V8\StringValue($isolate, $value); (V8\StringValue) is incompatible with the return type declared by the interface Pinepain\JsSandbox\Wrappers\WrapperInterface::wrap of type V8\Value|V8\PrimitiveVal...bject|V8\FunctionObject.

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...
49
        }
50
51
        if (is_int($value) || is_float($value)) {
52
            return new NumberValue($isolate, $value);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \V8\NumberValue($isolate, $value); (V8\NumberValue) is incompatible with the return type declared by the interface Pinepain\JsSandbox\Wrappers\WrapperInterface::wrap of type V8\Value|V8\PrimitiveVal...bject|V8\FunctionObject.

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...
53
        }
54
55
        throw new WrapperException('Vale type ' . gettype($value) . ' is not supported');
56
    }
57
}
58