Issues (48)

Soluble/Japha/Bridge/Driver/Pjb62/NativeParser.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * soluble-japha / PHPJavaBridge driver client.
6
 *
7
 * Refactored version of phpjababridge's Java.inc file compatible
8
 * with php java bridge 6.2
9
 *
10
 *
11
 * @credits   http://php-java-bridge.sourceforge.net/pjb/
12
 *
13
 * @see      http://github.com/belgattitude/soluble-japha
14
 *
15
 * @author Jost Boekemeier
16
 * @author Vanvelthem Sébastien (refactoring and fixes from original implementation)
17
 * @license   MIT
18
 *
19
 * The MIT License (MIT)
20
 * Copyright (c) 2014-2017 Jost Boekemeier
21
 * Permission is hereby granted, free of charge, to any person obtaining a copy
22
 * of this software and associated documentation files (the "Software"), to deal
23
 * in the Software without restriction, including without limitation the rights
24
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25
 * copies of the Software, and to permit persons to whom the Software is
26
 * furnished to do so, subject to the following conditions:
27
 *
28
 * The above copyright notice and this permission notice shall be included in
29
 * all copies or substantial portions of the Software.
30
 *
31
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37
 * THE SOFTWARE.
38
 */
39
40
namespace Soluble\Japha\Bridge\Driver\Pjb62;
41
42
class NativeParser implements ParserInterface
43
{
44
    /**
45
     * @var resource
46
     */
47
    protected $parser;
48
49
    /**
50
     * @var Client
51
     */
52
    protected $client;
53
54
    /**
55
     * @var int
56
     */
57
    protected $level;
58
59
    /**
60
     * @var bool
61
     */
62
    protected $event;
63
64
    /**
65
     * @var string
66
     */
67
    protected $buf;
68
69
    /**
70
     * @var int
71
     */
72
    protected $java_recv_size;
73
74
    /**
75
     * @param Client $client
76
     */
77 32
    public function __construct(Client $client)
78
    {
79 32
        $this->client = $client;
80 32
        $this->parser = xml_parser_create();
0 ignored issues
show
Documentation Bug introduced by
It seems like xml_parser_create() can also be of type XmlParser. However, the property $parser is declared as type resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
81 32
        xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
82 32
        xml_set_object($this->parser, $this);
83 32
        xml_set_element_handler($this->parser, 'begin', 'end');
84 32
        xml_parse($this->parser, '<F>');
85 32
        $this->level = 0;
86 32
        $this->java_recv_size = $client->java_recv_size;
87 32
    }
88
89
    /**
90
     * @param resource $parser
91
     * @param string   $name
92
     * @param mixed    $param
93
     */
94 113
    protected function begin($parser, $name, $param): void
95
    {
96 113
        $this->event = true;
97 113
        switch ($name) {
98 113
            case 'X':
99 113
            case 'A':
100 15
                ++$this->level;
101 15
                break;
102
        }
103 113
        $this->client->begin($name, $param);
104 113
    }
105
106
    /**
107
     * @param resource $parser
108
     * @param string   $name
109
     */
110 112
    public function end($parser, $name): void
111
    {
112 112
        $this->client->end($name);
113 112
        switch ($name) {
114 112
            case 'X':
115 112
            case 'A':
116 15
                --$this->level;
117
        }
118 112
    }
119
120
    /**
121
     * @param string $str
122
     *
123
     * @return string
124
     */
125 90
    public function getData(string $str): string
126
    {
127 90
        return base64_decode($str);
128
    }
129
130 114
    public function parse(): void
131
    {
132
        do {
133 114
            $this->event = false;
134 114
            $this->buf = $this->client->read($this->java_recv_size);
135 113
            $len = strlen($this->buf);
136 113
            if (!xml_parse($this->parser, $this->buf, $len === 0)) {
137 1
                $this->client->protocol->handler->shutdownBrokenConnection(
138 1
                    sprintf(
139 1
                        'protocol error: "buf: {%s}", %s at col %d. Check the back end log for OutOfMemoryErrors.',
140 1
                        $this->buf,
141 1
                        xml_error_string(xml_get_error_code($this->parser)),
142 1
                        xml_get_current_column_number($this->parser)
143
                    )
144
                );
145
            }
146 113
        } while (!$this->event || $this->level > 0);
147 113
    }
148
}
149