Failed Conditions
Push — master ( cd6515...587c3f )
by Sylvain
14:03
created

TransactionLines::writeItem()   C

Complexity

Conditions 12
Paths 4

Size

Total Lines 75
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 75
ccs 42
cts 42
cp 1
rs 6.9666
cc 12
nc 4
nop 1
crap 12

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Service\Exporter;
6
7
use Application\Model\TransactionLine;
8
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
9
10
class TransactionLines extends AbstractExcel
11
{
12 1
    public function __construct(string $hostname)
13
    {
14 1
        parent::__construct($hostname);
15
16 1
        $this->sheet->setTitle('Compta Écritures');
17
    }
18
19 1
    private ?int $currentTransactionRowStart = null;
20
21 1
    private ?int $currentTransactionId = null;
22
23
    protected function getTitleForFilename(): string
24 1
    {
25
        return 'compta_ecritures_' . date('Y-m-d-H:i:s');
26 1
    }
27 1
28 1
    protected function getHeaders(): array
29 1
    {
30 1
        return [
31 1
            ['label' => 'Date', 'width' => 12, 'formats' => [self::$headerFormat, self::$dateFormat], 'autofilter' => true],
32 1
            ['label' => 'ID', 'width' => 7, 'formats' => [self::$headerFormat, self::$centerFormat], 'autofilter' => true],
33 1
            ['label' => 'Transaction', 'width' => 25, 'formats' => [self::$headerFormat], 'autofilter' => true],
34 1
            ['label' => 'Remarques', 'width' => 25, 'formats' => [self::$headerFormat], 'autofilter' => true],
35 1
            ['label' => 'Remarques internes', 'width' => 25, 'formats' => [self::$headerFormat], 'autofilter' => true],
36 1
            ['label' => 'Écriture', 'width' => 25, 'formats' => [self::$headerFormat], 'autofilter' => true],
37 1
            ['label' => 'Remarques', 'width' => 25, 'formats' => [self::$headerFormat], 'autofilter' => true],
38 1
            ['label' => 'Réservable', 'width' => 30, 'formats' => [self::$headerFormat], 'autofilter' => true],
39 1
            ['label' => 'Compte débit', 'width' => 30, 'formats' => [self::$headerFormat], 'autofilter' => true],
40 1
            ['label' => 'Compte crédit', 'width' => 30, 'formats' => [self::$headerFormat], 'autofilter' => true],
41 1
            ['label' => 'Montant débit', 'width' => 20, 'formats' => [self::$headerFormat], 'autofilter' => true],
42
            ['label' => 'Montant crédit', 'width' => 20, 'formats' => [self::$headerFormat], 'autofilter' => true],
43
            ['label' => 'Pointé', 'width' => 12, 'formats' => [self::$headerFormat, self::$centerFormat], 'autofilter' => true],
44
            ['label' => 'Tag', 'width' => 30, 'formats' => [self::$headerFormat], 'autofilter' => true],
45
        ];
46
    }
47 1
48
    protected function initialize(string $path): void
49 1
    {
50 1
        parent::initialize($path);
51 1
        $this->writeHeaders($this->getHeaders());
52 1
        $this->firstDataRow = ++$this->row;
53
        $this->column = $this->firstDataColumn;
54 1
        $this->sheet->setShowGridlines(false);
55 1
    }
56 1
57 1
    /**
58 1
     * @param TransactionLine $line
59 1
     */
60
    protected function writeItem($line): void
61 1
    {
62 1
        $mergeRowsFromColumns = [
63 1
            $this->firstDataColumn + 1, // Transaction.ID
64
            $this->firstDataColumn + 2, // Transaction.name
65 1
            $this->firstDataColumn + 3, // Transaction.remarks
66 1
            $this->firstDataColumn + 4, // Transaction.internalRemarks
67 1
        ];
68 1
69
        $transaction = $line->getTransaction();
70 1
        $transactionId = $transaction->getId();
71
72
        if (!$this->currentTransactionId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->currentTransactionId of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
73 1
            $this->currentTransactionId = $transactionId;
74 1
            $this->currentTransactionRowStart = $this->row;
75
        } elseif ($transactionId !== $this->currentTransactionId) {
76
            // Current line starts a new transaction
77 1
            if ($this->currentTransactionRowStart < $this->row - 1) {
78 1
                // Multi-line transaction
79
                // Merge cells that have the same value because from the same transaction
80
                foreach ($mergeRowsFromColumns as $column) {
81
                    $this->sheet->mergeCells([$column, $this->currentTransactionRowStart, $column, $this->row - 1]);
82 1
                }
83
            }
84
            $this->currentTransactionRowStart = $this->row;
85 1
            $this->currentTransactionId = $transactionId;
86 1
        }
87 1
88
        // Date
89 1
        $this->write($line->getTransactionDate()->format('d.m.Y'));
90
91 1
        // Transaction.ID
92
        $this->write($transactionId);
93 1
        $url = 'https://' . $this->hostname . '/admin/transaction/%u';
94
        $this->sheet->getCell([$this->column - 1, $this->row])->getHyperlink()->setUrl(sprintf($url, $transactionId));
95
        // Transaction.name
96 1
        $this->write($transaction->getName());
97
        // Transaction.remarks
98 1
        $this->write($transaction->getRemarks());
99
        // Transaction.internalRemarks
100 1
        $this->write($transaction->getInternalRemarks());
101 1
102
        // TransactionLine.name
103 1
        $this->write($line->getName());
104 1
105
        // TransactionLine.remarks
106 1
        $this->write($line->getRemarks());
107 1
        // Bookable.name
108
        $bookable = $line->getBookable();
109 1
        $this->write($bookable ? $bookable->getName() : '');
110
        // Debit account
111 1
        $debit = $line->getDebit();
112
        $this->write($debit ? implode(' ', [$debit->getCode(), $debit->getName()]) : '');
113 1
        // Credit account
114 1
        $credit = $line->getCredit();
115
        $this->write($credit ? implode(' ', [$credit->getCode(), $credit->getName()]) : '');
116 1
        // Debit amount
117 1
        $this->write($debit ? $this->moneyFormatter->format($line->getBalance()) : '');
118
        // Credit amount
119 1
        $this->write($credit ? $this->moneyFormatter->format($line->getBalance()) : '');
120
        // Reconciled
121 1
        $this->sheet->getStyle([$this->column, $this->row])->applyFromArray(self::$centerFormat);
122
        $this->write($line->isReconciled() ? '✔️' : '');
123 1
        // Tag
124 1
        $tag = $line->getTransactionTag();
125
        $this->write($tag ? $tag->getName() : '');
126 1
127
        $this->lastDataColumn = $this->column - 1;
128 1
129
        $range = Coordinate::stringFromColumnIndex($this->firstDataColumn) . $this->row . ':' . Coordinate::stringFromColumnIndex($this->lastDataColumn) . $this->row;
130 1
        // Thicker horizontal delimiter between lines of different transactions
131 1
        $this->sheet->getStyle($range)->applyFromArray(self::$bordersBottomLight);
132
133 1
        $this->column = $this->firstDataColumn;
134
        ++$this->row;
135
    }
136 1
137
    protected function writeFooter(): void
138 1
    {
139
        // Date
140
        $this->write('');
141 1
        // ID
142
        $this->write('');
143 1
        // Transaction.name
144
        $this->write('');
145 1
        // Transaction.remarks
146
        $this->write('');
147 1
        // Internal remarks
148
        $this->write('');
149 1
        // TransactionLine.name
150
        $this->write('');
151 1
        // Remarks
152
        $this->write('');
153 1
        // Bookable.name
154
        $this->write('');
155 1
        // Debit account
156
        $this->write('');
157 1
        // Credit account
158
        $this->write('');
159 1
        // Debit amount
160
        $this->write('=SUBTOTAL(9,' . Coordinate::stringFromColumnIndex($this->column) . '2:' . Coordinate::stringFromColumnIndex($this->column) . ($this->row - 1) . ')');
161 1
        // Credit amount
162
        $this->write('=SUBTOTAL(9,' . Coordinate::stringFromColumnIndex($this->column) . '2:' . Coordinate::stringFromColumnIndex($this->column) . ($this->row - 1) . ')');
163 1
        // Reconciled
164
        $this->write('');
165 1
166
        // Apply style
167
        $range = Coordinate::stringFromColumnIndex($this->firstDataColumn) . $this->row . ':' . Coordinate::stringFromColumnIndex($this->column - 1) . $this->row;
168 1
        $this->sheet->getStyle($range)->applyFromArray(self::$totalFormat);
169 1
    }
170
}
171