Completed
Pull Request — master (#178)
by ignace nyamagana
02:34
created

Header::getHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
* This file is part of the League.csv library
4
*
5
* @license http://opensource.org/licenses/MIT
6
* @link https://github.com/thephpleague/csv/
7
* @version 9.0.0
8
* @package League.csv
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
namespace League\Csv\Config;
14
15
use InvalidArgumentException;
16
use LimitIterator;
17
18
/**
19
 * Trait to configure the CSV header
20
 *
21
 * @package League.csv
22
 * @since  9.0.0
23
 *
24
 */
25
trait Header
26
{
27
    /**
28
     * Csv Header Info
29
     *
30
     * @var array|int
31
     */
32
    protected $header = [];
33
34
    /**
35
     * Tell whether the current header is internal
36
     * or user submitted
37
     *
38
     * @return int|null
39
     */
40
    public function getHeaderOffset()
41
    {
42
        if (is_array($this->header)) {
43
            return null;
44
        }
45
46
        return $this->header;
47
    }
48
49
    /**
50
     * Returns the CSV header
51
     *
52
     * @return array
53
     */
54
    public function getHeader()
55
    {
56
        if (is_array($this->header)) {
57
            return $this->header;
58
        }
59
60
        return $this->getHeaderFromDocument();
61
    }
62
63
    /**
64
     * Get the Header from a CSV record
65
     *
66
     * @return array
67
     */
68
    protected function getHeaderFromDocument()
69
    {
70
        $iterator = new LimitIterator($this->getIterator(), $this->header);
0 ignored issues
show
Bug introduced by
It seems like getIterator() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
71
        $iterator->rewind();
72
        $header = $iterator->current();
73
        if ($iterator->key() !== $this->header) {
74
            throw new InvalidArgumentException('the select offset does not exist');
75
        }
76
77
        if (0 !== $this->header) {
78
            return $header;
79
        }
80
81
        return $this->formatHeader($header, $this->getInputBOM(), $this->getEnclosure());
0 ignored issues
show
Bug introduced by
It seems like getInputBOM() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like getEnclosure() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
82
    }
83
84
    /**
85
     * Format the Document Header
86
     *
87
     * @param string[] $header
88
     * @param string   $bom
89
     * @param string   $enclosure
90
     *
91
     * @return string[]
92
     */
93
    protected function formatHeader(array $header, $bom, $enclosure)
94
    {
95
        $header = $this->stripBOM($header, $bom, $enclosure);
0 ignored issues
show
Bug introduced by
It seems like stripBOM() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
96
        if (!$this->useInternalConverter($this)) {
0 ignored issues
show
Bug introduced by
It seems like useInternalConverter() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
97
            return $header;
98
        }
99
100
        return $this->convertRecordToUtf8($header, $this->getInputEncoding());
0 ignored issues
show
Bug introduced by
It seems like getInputEncoding() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like convertRecordToUtf8() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
101
    }
102
103
    /**
104
     * Selects the array to be used as key for the fetchAssoc method
105
     *
106
     * @param int|null|array $offset_or_keys the assoc key OR the row Index to be used
107
     *                                       as the key index
108
     *
109
     * @return $this
110
     */
111
    public function setHeader($offset_or_keys)
112
    {
113
        if (is_array($offset_or_keys)) {
114
            $this->header = $this->validateHeader($offset_or_keys);
0 ignored issues
show
Bug introduced by
It seems like validateHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
115
            return $this;
116
        }
117
118
        if (null === $offset_or_keys) {
119
            $this->header = [];
120
            return $this;
121
        }
122
123
        $this->header = $this->validateInteger($offset_or_keys, 0, 'the header offset is invalid');
0 ignored issues
show
Bug introduced by
It seems like validateInteger() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
124
        return $this;
125
    }
126
}
127