Header::renderInternal()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 8.6737
cc 5
eloc 15
nc 3
nop 0
1
<?php
2
/**
3
 * Render the header
4
 *
5
 * @author  Tim Lochmüller
6
 */
7
8
namespace FRUIT\Ink\Rendering;
9
10
use FRUIT\Ink\Configuration;
11
use TYPO3\CMS\Core\Utility\GeneralUtility;
12
use TYPO3\CMS\Core\Utility\MailUtility;
13
14
/**
15
 * Render the header
16
 */
17
class Header extends AbstractRendering
18
{
19
20
    /**
21
     * Render the given element
22
     *
23
     * @return array
24
     */
25
    public function renderInternal()
26
    {
27
        $headerWrap = MailUtility::breakLinesForEmail(trim($this->contentObject->data['header']), LF, Configuration::getPlainTextWith());
28
        $subHeaderWrap = MailUtility::breakLinesForEmail(trim($this->contentObject->data['subheader']), LF, Configuration::getPlainTextWith());
29
30
        // align
31
        $header = array_merge(GeneralUtility::trimExplode(LF, $headerWrap, true), GeneralUtility::trimExplode(LF, $subHeaderWrap, true));
32
        if ($this->contentObject->data['header_position'] == 'right') {
33
            foreach ($header as $key => $l) {
34
                $l = trim($l);
35
                $header[$key] = str_pad(' ', (Configuration::getPlainTextWith() - strlen($l)), ' ', STR_PAD_LEFT) . $l;
36
            }
37
        } elseif ($this->contentObject->data['header_position'] == 'center') {
38
            foreach ($header as $key => $l) {
39
                $l = trim($l);
40
                $header[$key] = str_pad(' ', floor((Configuration::getPlainTextWith() - strlen($l)) / 2), ' ', STR_PAD_LEFT) . $l;
41
            }
42
        }
43
        $header = implode(LF, $header);
44
        $lines[] = $header;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$lines was never initialized. Although not strictly required by PHP, it is generally a good practice to add $lines = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
45
        return $lines;
46
    }
47
}
48