Completed
Pull Request — develop (#27)
by Chris
01:41
created

PlaylistFacade   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 40.68 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 24
loc 59
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A parse() 0 6 1
A dump() 12 12 1
A parseFromFile() 0 6 1
A dumpToFile() 12 12 1
A checkPlaylist() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Chrisyue\PhpM3u8\M3u8;
4
5
use Chrisyue\PhpM3u8\M3u8\Core\Playlist;
6
use Chrisyue\PhpM3u8\M3u8\Lines\Lines;
7
use Chrisyue\PhpM3u8\M3u8\PropertyReader\Reader;
8
use Chrisyue\PhpM3u8\M3u8\Transformer\TagInfo;
9
use Chrisyue\PhpM3u8\Stream\FileStream;
10
use Chrisyue\PhpM3u8\Stream\TextStream;
11
use Chrisyue\PhpM3u8\M3u8\Builder\ObjectBuilder;
12
13
class PlaylistFacade
14
{
15
    private $core;
16
17
    public function __construct($class)
18
    {
19
        $this->core = new Playlist();
20
        $this->core->class = $class;
21
        $this->core->reader = new Reader();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Chrisyue\PhpM3u8\M3u8\PropertyReader\Reader() of type object<Chrisyue\PhpM3u8\...\PropertyReader\Reader> is incompatible with the declared type object<Chrisyue\PhpM3u8\...ropertyReaderInterface> of property $reader.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
22
        $this->core->builder = new ObjectBuilder();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Chrisyue\PhpM3u8\M3u8\Builder\ObjectBuilder() of type object<Chrisyue\PhpM3u8\...\Builder\ObjectBuilder> is incompatible with the declared type object<Chrisyue\PhpM3u8\...ilder\BuilderInterface> of property $builder.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
23
    }
24
25
    public function parse($text)
26
    {
27
        $lines = new Lines(new TextStream($text), new TagInfo());
28
29
        return $this->core->setLines($lines)->parse();
30
    }
31
32 View Code Duplication
    public function dump($playlist)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34
        $this->checkPlaylist($playlist);
35
36
        $text = new TextStream();
37
        $lines = new Lines($text, new TagInfo());
38
39
        $this->core->setLines($lines);
40
        $this->core->dump($playlist);
41
42
        return $text;
43
    }
44
45
    public function parseFromFile($path)
46
    {
47
        $lines = new Lines(new FileStream(new \SplFileObject($path)), new TagInfo());
48
49
        return $this->core->setLines($lines)->parse();
50
    }
51
52 View Code Duplication
    public function dumpToFile($playlist, $path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $this->checkPlaylist($playlist);
55
56
        $file = new \SplFileObject($path);
57
        $lines = new Lines(new FileStream($file, new TagInfo()));
0 ignored issues
show
Unused Code introduced by
The call to FileStream::__construct() has too many arguments starting with new \Chrisyue\PhpM3u8\M3u8\Transformer\TagInfo().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
The call to Lines::__construct() misses a required argument $transformer.

This check looks for function calls that miss required arguments.

Loading history...
58
59
        $this->core->setLines($lines);
60
        $this->core->dump($playlist);
61
62
        return $file;
63
    }
64
65
    private function checkPlaylist($playlist)
66
    {
67
        if (!$playlist instanceof $this->class) {
68
            throw new \InvalidArgumentException('Only playlist of class `%s` is supported, `%s` is given', $this->class, get_class($playlist));
0 ignored issues
show
Bug introduced by
The property class does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
        }
70
    }
71
}
72