GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Writer::write()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
/**
3
 * This class contains the Writer Class
4
 *
5
 * PHP version 5.3+
6
 *
7
 * @package   Writer
8
 * @author    Gabriel Alonso <[email protected]>
9
 * @copyright 2015 Galonso
10
 * @license   WTFPL - http://www.wtfpl.net/txt/copying/
11
 * @link      https://github.com/g-alonso/FixedLengthHelper
12
 */
13
namespace Galonso\FixedLengthHelper;
14
15
use RuntimeException;
16
17
/**
18
 * Writer
19
 *
20
 * @package   Writer
21
 * @author    Gabriel Alonso <[email protected]>
22
 * @copyright 2015 Galonso
23
 * @license   WTFPL - http://www.wtfpl.net/txt/copying/
24
 * @link      https://github.com/g-alonso/FixedLengthHelper
25
 */
26
class Writer
27
{
28
    /**
29
     * Data
30
     * @var array
31
     */
32
    private $data;
33
34
    /**
35
     * Map
36
     * @var array
37
     */
38
    private $map = array();
39
40
    /**
41
     * Constructor
42
     *
43
     * @param array $data
44
     * @param array $map
45
     * @throws \Exception
46
     */
47
    public function __construct($data, $map)
48
    {
49
        $this->data = $data;
50
        $this->map  = $map;
51
52
        foreach ($this->data as $k => $data) {
53
            foreach ($this->map as $field => $length) {
54
                if (array_key_exists($field, $data) === false) {
55
                    throw new \Exception("Invalid configuration. Field $field not found.");
56
                }
57
            }
58
        }
59
    }
60
61
    /**
62
     * Write
63
     *
64
     * @param $file
65
     * @return true
66
     * @throws \Exception
67
     */
68
    public function write($file)
69
    {
70
        if (!is_writable(dirname($file))) {
71
            throw new RuntimeException("Could not write!");
72
        }
73
74
        $content = "";
75
76
        foreach ($this->data as $k => $v) {
77
            foreach ($this->map as $field => $length) {
78
                $content .= str_pad($v[$field], $length, " ", STR_PAD_RIGHT);
79
            }
80
            $content .= "\n";
81
        }
82
83
        $fp = fopen($file, 'w+');
84
        fputs($fp, $content);
85
        fclose($fp);
86
87
        return true;
88
    }
89
}
90