Completed
Push — develop ( 29208e...50a0ec )
by Adrien
05:34
created

ChunkReadFilter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 27
loc 27
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A readCell() 9 9 4

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 8.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Sample;
4
5
use PhpOffice\PhpSpreadsheet\IOFactory;
6
use PhpOffice\PhpSpreadsheet\Reader\IReadFilter;
7
8
require __DIR__ . '/../Header.php';
9
10
$inputFileType = 'Xls';
11
$inputFileName = __DIR__ . '/sampleData/example2.xls';
12
13
/**  Define a Read Filter class implementing IReadFilter  */
14 View Code Duplication
class ChunkReadFilter implements IReadFilter
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...
15
{
16
    private $_startRow = 0;
17
    private $_endRow = 0;
18
19
    /**
20
     * We expect a list of the rows that we want to read to be passed into the constructor.
21
     *
22
     * @param mixed $startRow
23
     * @param mixed $chunkSize
24
     */
25
    public function __construct($startRow, $chunkSize)
26
    {
27
        $this->_startRow = $startRow;
28
        $this->_endRow = $startRow + $chunkSize;
29
    }
30
31
    public function readCell($column, $row, $worksheetName = '')
32
    {
33
        //  Only read the heading row, and the rows that were configured in the constructor
34
        if (($row == 1) || ($row >= $this->_startRow && $row < $this->_endRow)) {
35
            return true;
36
        }
37
38
        return false;
39
    }
40
}
41
42
$helper->log('Loading file ' . pathinfo($inputFileName, PATHINFO_BASENAME) . ' using IOFactory with a defined reader type of ' . $inputFileType);
43
/*  Create a new Reader of the type defined in $inputFileType  * */
44
$reader = IOFactory::createReader($inputFileType);
45
46
/*  Define how many rows we want for each "chunk"  * */
47
$chunkSize = 20;
48
49
/*  Loop to read our worksheet in "chunk size" blocks  * */
50
for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) {
51
    $helper->log('Loading WorkSheet using configurable filter for headings row 1 and for rows ' . $startRow . ' to ' . ($startRow + $chunkSize - 1));
52
    /*  Create a new Instance of our Read Filter, passing in the limits on which rows we want to read  * */
53
    $chunkFilter = new ChunkReadFilter($startRow, $chunkSize);
54
    /*  Tell the Reader that we want to use the new Read Filter that we've just Instantiated  * */
55
    $reader->setReadFilter($chunkFilter);
56
    /*  Load only the rows that match our filter from $inputFileName to a PhpSpreadsheet Object  * */
57
    $spreadsheet = $reader->load($inputFileName);
58
59
    //	Do some processing here
60
61
    $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
62
    var_dump($sheetData);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($sheetData); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
63
}
64