GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#76)
by Tom
02:00
created

HtmlDriver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 27
loc 27
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 14 14 2
A extension() 4 4 1
A match() 4 4 1

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 Spatie\Snapshots\Drivers;
4
5
use DOMDocument;
6
use PHPUnit\Framework\Assert;
7
use Spatie\Snapshots\Driver;
8
use Spatie\Snapshots\Exceptions\CantBeSerialized;
9
10 View Code Duplication
class HtmlDriver implements Driver
0 ignored issues
show
Duplication introduced by
This class 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...
11
{
12
    public function serialize($data): string
13
    {
14
        if (! is_string($data)) {
15
            throw new CantBeSerialized('Only strings can be serialized to html');
16
        }
17
18
        $domDocument = new DOMDocument('1.0');
19
        $domDocument->preserveWhiteSpace = false;
20
        $domDocument->formatOutput = true;
21
22
        @$domDocument->loadHTML($data); // to ignore HTML5 errors
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
23
24
        return $domDocument->saveHTML();
25
    }
26
27
    public function extension(): string
28
    {
29
        return 'html';
30
    }
31
32
    public function match($expected, $actual)
33
    {
34
        Assert::assertEquals($expected, $this->serialize($actual));
35
    }
36
}
37