StreamWriter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 78.95%

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
ccs 15
cts 19
cp 0.7895
wmc 8
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 4
A __destruct() 0 7 3
A write() 0 4 1
1
<?php
2
/**
3
 * This file is part of PHP Mess Detector.
4
 *
5
 * Copyright (c) Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Licensed under BSD License
9
 * For full copyright and license information, please see the LICENSE file.
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @author Manuel Pichler <[email protected]>
13
 * @copyright Manuel Pichler. All rights reserved.
14
 * @license https://opensource.org/licenses/bsd-license.php BSD License
15
 * @link http://phpmd.org/
16
 */
17
18
namespace PHPMD\Writer;
19
20
use PHPMD\AbstractWriter;
21
22
/**
23
 * This writer uses PHP's stream api as its output target.
24
 */
25
class StreamWriter extends AbstractWriter
26
{
27
    /**
28
     * The stream resource handle
29
     *
30
     * @var resource
31
     */
32
    private $stream = null;
33
34
    /**
35
     * Constructs a new stream writer instance.
36
     *
37
     * @param resource|string $streamResourceOrUri
38
     */
39 4
    public function __construct($streamResourceOrUri)
40
    {
41 4
        if (is_resource($streamResourceOrUri) === true) {
42
            $this->stream = $streamResourceOrUri;
43
        } else {
44 4
            $dirName = dirname($streamResourceOrUri);
45 4
            if (file_exists($dirName) === false) {
46
                mkdir($dirName, 0777, true);
47
            }
48 4
            if (file_exists($dirName) === false) {
49
                $message = 'Cannot find output directory "' . $dirName . '".';
50
                throw new \RuntimeException($message);
51
            }
52
53 4
            $this->stream = fopen($streamResourceOrUri, 'wb');
54
        }
55 4
    }
56
57
    /**
58
     * The dtor closes the open output resource.
59
     */
60 4
    public function __destruct()
61
    {
62 4
        if ($this->stream !== STDOUT && is_resource($this->stream) === true) {
63 4
            @fclose($this->stream);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
64
        }
65 4
        $this->stream = null;
66 4
    }
67
68
    /**
69
     * Writes the given <b>$data</b> fragment to the wrapper output stream.
70
     *
71
     * @param string $data
72
     * @return void
73
     */
74 4
    public function write($data)
75
    {
76 4
        fwrite($this->stream, $data);
77 4
    }
78
}
79