Passed
Pull Request — master (#10)
by Joao
01:57
created

BaseFormatter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 6
c 1
b 0
f 0
dl 0
loc 25
ccs 6
cts 7
cp 0.8571
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 3
A saveToFile() 0 3 1
1
<?php
2
3
namespace ByJG\AnyDataset\Core\Formatter;
4
5
use \ByJG\AnyDataset\Core\AnyDataset;
6
use \ByJG\AnyDataset\Core\Row;
7
use InvalidArgumentException;
8
9
abstract class BaseFormatter
10
{
11
    /**
12
     * @var AnyDataset
13
     */
14
    protected $object;
15
16
    abstract public function raw();
17
18
    abstract public function toText();
19
20 2
    public function saveToFile($filename)
21
    {
22 2
        file_put_contents($filename, $this->toText());
23 2
    }
24
25
    /**
26
     * $object AnyDataset|Row
27
     */
28 7
    public function __construct($object)
29
    {
30 7
        if (!($object instanceof AnyDataset) && !($object instanceof Row)) {
31
            throw new InvalidArgumentException("Constructor must have an AnyDataset or Row instance in the argument");
32
        }
33 7
        $this->object = $object;
0 ignored issues
show
Documentation Bug introduced by
It seems like $object can also be of type ByJG\AnyDataset\Core\Row. However, the property $object is declared as type ByJG\AnyDataset\Core\AnyDataset. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
34
    }
35
}