HtmlDataTables   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 19.61 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
dl 10
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A newData() 0 3 1
A boot() 8 8 1
A toArray() 0 22 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 Ghc\Rosetta\Messages;
4
5
use Ghc\Rosetta\Item;
6
use Ghc\Rosetta\Transformers\DataTable;
7
use Symfony\Component\DomCrawler\Crawler;
8
9
class HtmlDataTables extends Xml
10
{
11
    /**
12
     * Boot Transformer.
13
     */
14 View Code Duplication
    protected function boot()
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...
15
    {
16
        $this->addDefaultConfig([
17
            'selector'         => 'table',
18
            'hasHeader'        => true,
19
            'useHeaderAsIndex' => true,
20
            'ignoreHeader'     => false,
21
            'orientation'      => 'vertical',
22
        ]);
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function newData()
29
    {
30
        return '<html></html>';
31
    }
32
33
    /**
34
     * Get the instance as an array.
35
     *
36
     * @return array
37
     */
38
    public function toArray()
39
    {
40
        $crawler = new Crawler((string) $this->getData());
41
42
        return $crawler->filter($this->getConfig()['selector'])->each(function ($table) {
43
            $rows = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $rows is dead and can be removed.
Loading history...
44
45
            try {
46
                /** @var Crawler $table */
47
                $rows = $table->filter('tr')->each(function ($row) {
48
                    /* @var Crawler $row */
49
                    return $row->filter('th,td')->each(function ($column) {
50
                        /* @var Crawler $column */
51
                        return trim($column->text());
52
                    });
53
                });
54
55
                $rows = (new Item($rows, [new DataTable($this->getConfig())]))->toArray();
56
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
57
            }
58
59
            return $rows;
60
        });
61
    }
62
}
63