Passed
Push — master ( 902a34...c826a7 )
by Kyle
53s queued 11s
created

src/main/php/PHPMD/Writer/StreamWriter.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
     * @throws \RuntimeException If the output directory cannot be found.
39 4
     */
40
    public function __construct($streamResourceOrUri)
41 4
    {
42
        if (is_resource($streamResourceOrUri) === true) {
43
            $this->stream = $streamResourceOrUri;
44
45 4
            return;
46 4
        }
47
        $dirName = dirname($streamResourceOrUri);
48
        if (file_exists($dirName) === false) {
49 4
            mkdir($dirName, 0777, true);
50
        }
51
        if (file_exists($dirName) === false) {
52
            $message = 'Cannot find output directory "' . $dirName . '".';
53
            throw new \RuntimeException($message);
54 4
        }
55 4
56
        $this->stream = fopen($streamResourceOrUri, 'wb');
57
    }
58
59
    /**
60 4
     * The dtor closes the open output resource.
61
     */
62 4
    public function __destruct()
63 4
    {
64
        if ($this->stream !== STDOUT && is_resource($this->stream) === true) {
65 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...
66 4
        }
67
        $this->stream = null;
68
    }
69
70
    /**
71
     * Writes the given <b>$data</b> fragment to the wrapper output stream.
72
     *
73
     * @param string $data
74 4
     * @return void
75
     */
76 4
    public function write($data)
77 4
    {
78
        fwrite($this->stream, $data);
79
    }
80
81
    /**
82
     * @return resource
83
     */
84
    public function getStream()
85
    {
86
        return $this->stream;
87
    }
88
}
89