Skip to content

Commit 63362eb

Browse files
committed
fix for #842
1 parent 4c3dbc3 commit 63362eb

File tree

5 files changed

+55
-81
lines changed

5 files changed

+55
-81
lines changed

api.include.php

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,18 +1515,13 @@ public function createStream(string $content = ''): StreamInterface
15151515

15161516
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
15171517
{
1518-
try {
1519-
$resource = @\fopen($filename, $mode);
1520-
} catch (\Throwable $e) {
1521-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
1522-
}
1523-
1518+
$resource = @\fopen($filename, $mode);
15241519
if (false === $resource) {
1525-
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
1526-
throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.', $mode));
1520+
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) {
1521+
throw new \InvalidArgumentException('The mode ' . $mode . ' is invalid.');
15271522
}
15281523

1529-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
1524+
throw new \RuntimeException('The file ' . $filename . ' cannot be opened.');
15301525
}
15311526

15321527
return Stream::create($resource);
@@ -2382,7 +2377,7 @@ public function seek($offset, $whence = \SEEK_SET) /*:void*/
23822377
}
23832378

23842379
if (-1 === \fseek($this->stream, $offset, $whence)) {
2385-
throw new \RuntimeException('Unable to seek to stream position "' . $offset . '" with whence ' . \var_export($whence, true));
2380+
throw new \RuntimeException('Unable to seek to stream position ' . $offset . ' with whence ' . \var_export($whence, true));
23862381
}
23872382
}
23882383

@@ -2573,11 +2568,9 @@ public function getStream(): StreamInterface
25732568
return $this->stream;
25742569
}
25752570

2576-
try {
2577-
return Stream::create(\fopen($this->file, 'r'));
2578-
} catch (\Throwable $e) {
2579-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $this->file));
2580-
}
2571+
$resource = \fopen($this->file, 'r');
2572+
2573+
return Stream::create($resource);
25812574
}
25822575

25832576
public function moveTo($targetPath) /*:void*/
@@ -2596,13 +2589,8 @@ public function moveTo($targetPath) /*:void*/
25962589
$stream->rewind();
25972590
}
25982591

2599-
try {
2600-
// Copy the contents of a stream into another stream until end-of-file.
2601-
$dest = Stream::create(\fopen($targetPath, 'w'));
2602-
} catch (\Throwable $e) {
2603-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $targetPath));
2604-
}
2605-
2592+
// Copy the contents of a stream into another stream until end-of-file.
2593+
$dest = Stream::create(\fopen($targetPath, 'w'));
26062594
while (!$stream->eof()) {
26072595
if (!$dest->write($stream->read(1048576))) {
26082596
break;
@@ -2613,7 +2601,7 @@ public function moveTo($targetPath) /*:void*/
26132601
}
26142602

26152603
if (false === $this->moved) {
2616-
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s"', $targetPath));
2604+
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to %s', $targetPath));
26172605
}
26182606
}
26192607

@@ -2688,7 +2676,7 @@ public function __construct(string $uri = '')
26882676
{
26892677
if ('' !== $uri) {
26902678
if (false === $parts = \parse_url($uri)) {
2691-
throw new \InvalidArgumentException(\sprintf('Unable to parse URI: "%s"', $uri));
2679+
throw new \InvalidArgumentException("Unable to parse URI: $uri");
26922680
}
26932681

26942682
// Apply parse_url parts to a URI.
@@ -6522,7 +6510,7 @@ public function __construct(string $dsn, /*?string*/ $user = null, /*?string*/ $
65226510
// explicitly NOT calling super::__construct
65236511
}
65246512

6525-
public function addInitCommand(string $command)/*: void*/
6513+
public function addInitCommand(string $command) /*: void*/
65266514
{
65276515
$this->commands[] = $command;
65286516
}
@@ -6607,17 +6595,17 @@ public function prepare($statement, $options = array())
66076595
return $this->pdo()->prepare($statement, $options);
66086596
}
66096597

6610-
public function quote($string, $parameter_type = null): string
6598+
public function quote($string, $parameter_type = \PDO::PARAM_STR): string
66116599
{
66126600
return $this->pdo()->quote($string, $parameter_type);
66136601
}
66146602

6615-
public function lastInsertId(/* ?string */$name = null): string
6603+
public function lastInsertId( /* ?string */$name = null): string
66166604
{
66176605
return $this->pdo()->lastInsertId($name);
66186606
}
66196607

6620-
public function query($query, /* ?int */$fetchMode = null, ...$fetchModeArgs): \PDOStatement
6608+
public function query($query, /* ?int */ $fetchMode = null, ...$fetchModeArgs): \PDOStatement
66216609
{
66226610
return call_user_func_array(array($this->pdo(), 'query'), func_get_args());
66236611
}
@@ -9106,7 +9094,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
91069094
use Tqdev\PhpCrudApi\Controller\Responder;
91079095
use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
91089096
use Tqdev\PhpCrudApi\Middleware\Router\Router;
9109-
use Tqdev\PhpCrudApi\RequestUtils;
91109097
use Tqdev\PhpCrudApi\ResponseFactory;
91119098

91129099
class XmlMiddleware extends Middleware
@@ -9163,7 +9150,7 @@ private function json2xml($json, $types = 'null,boolean,number,string,object,arr
91639150
if ($t($v) == 'boolean') {
91649151
$va->appendChild($d->createTextNode($v ? 'true' : 'false'));
91659152
} else {
9166-
$va->appendChild($d->createTextNode($v));
9153+
$va->appendChild($d->createTextNode((string) $v));
91679154
}
91689155
$ch = $c->appendChild($va);
91699156
if (in_array($t($v), $ts)) {
@@ -9178,15 +9165,15 @@ private function json2xml($json, $types = 'null,boolean,number,string,object,arr
91789165
return $d->saveXML($d->documentElement);
91799166
}
91809167

9181-
private function xml2json($xml)
9168+
private function xml2json($xml): string
91829169
{
91839170
$o = @simplexml_load_string($xml);
9184-
if ($o===false) {
9185-
return null;
9171+
if ($o === false) {
9172+
return '';
91869173
}
91879174
$a = @dom_import_simplexml($o);
91889175
if (!$a) {
9189-
return null;
9176+
return '';
91909177
}
91919178
$t = function ($v) {
91929179
$t = $v->getAttribute('type');
@@ -9224,7 +9211,7 @@ private function xml2json($xml)
92249211
return $c;
92259212
};
92269213
$c = $f($f, $a);
9227-
return json_encode($c);
9214+
return (string) json_encode($c);
92289215
}
92299216

92309217
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface

api.php

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,18 +1515,13 @@ public function createStream(string $content = ''): StreamInterface
15151515

15161516
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
15171517
{
1518-
try {
1519-
$resource = @\fopen($filename, $mode);
1520-
} catch (\Throwable $e) {
1521-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
1522-
}
1523-
1518+
$resource = @\fopen($filename, $mode);
15241519
if (false === $resource) {
1525-
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
1526-
throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.', $mode));
1520+
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'])) {
1521+
throw new \InvalidArgumentException('The mode ' . $mode . ' is invalid.');
15271522
}
15281523

1529-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
1524+
throw new \RuntimeException('The file ' . $filename . ' cannot be opened.');
15301525
}
15311526

15321527
return Stream::create($resource);
@@ -2382,7 +2377,7 @@ public function seek($offset, $whence = \SEEK_SET) /*:void*/
23822377
}
23832378

23842379
if (-1 === \fseek($this->stream, $offset, $whence)) {
2385-
throw new \RuntimeException('Unable to seek to stream position "' . $offset . '" with whence ' . \var_export($whence, true));
2380+
throw new \RuntimeException('Unable to seek to stream position ' . $offset . ' with whence ' . \var_export($whence, true));
23862381
}
23872382
}
23882383

@@ -2573,11 +2568,9 @@ public function getStream(): StreamInterface
25732568
return $this->stream;
25742569
}
25752570

2576-
try {
2577-
return Stream::create(\fopen($this->file, 'r'));
2578-
} catch (\Throwable $e) {
2579-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $this->file));
2580-
}
2571+
$resource = \fopen($this->file, 'r');
2572+
2573+
return Stream::create($resource);
25812574
}
25822575

25832576
public function moveTo($targetPath) /*:void*/
@@ -2596,13 +2589,8 @@ public function moveTo($targetPath) /*:void*/
25962589
$stream->rewind();
25972590
}
25982591

2599-
try {
2600-
// Copy the contents of a stream into another stream until end-of-file.
2601-
$dest = Stream::create(\fopen($targetPath, 'w'));
2602-
} catch (\Throwable $e) {
2603-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $targetPath));
2604-
}
2605-
2592+
// Copy the contents of a stream into another stream until end-of-file.
2593+
$dest = Stream::create(\fopen($targetPath, 'w'));
26062594
while (!$stream->eof()) {
26072595
if (!$dest->write($stream->read(1048576))) {
26082596
break;
@@ -2613,7 +2601,7 @@ public function moveTo($targetPath) /*:void*/
26132601
}
26142602

26152603
if (false === $this->moved) {
2616-
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s"', $targetPath));
2604+
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to %s', $targetPath));
26172605
}
26182606
}
26192607

@@ -2688,7 +2676,7 @@ public function __construct(string $uri = '')
26882676
{
26892677
if ('' !== $uri) {
26902678
if (false === $parts = \parse_url($uri)) {
2691-
throw new \InvalidArgumentException(\sprintf('Unable to parse URI: "%s"', $uri));
2679+
throw new \InvalidArgumentException("Unable to parse URI: $uri");
26922680
}
26932681

26942682
// Apply parse_url parts to a URI.
@@ -6522,7 +6510,7 @@ public function __construct(string $dsn, /*?string*/ $user = null, /*?string*/ $
65226510
// explicitly NOT calling super::__construct
65236511
}
65246512

6525-
public function addInitCommand(string $command)/*: void*/
6513+
public function addInitCommand(string $command) /*: void*/
65266514
{
65276515
$this->commands[] = $command;
65286516
}
@@ -6607,17 +6595,17 @@ public function prepare($statement, $options = array())
66076595
return $this->pdo()->prepare($statement, $options);
66086596
}
66096597

6610-
public function quote($string, $parameter_type = null): string
6598+
public function quote($string, $parameter_type = \PDO::PARAM_STR): string
66116599
{
66126600
return $this->pdo()->quote($string, $parameter_type);
66136601
}
66146602

6615-
public function lastInsertId(/* ?string */$name = null): string
6603+
public function lastInsertId( /* ?string */$name = null): string
66166604
{
66176605
return $this->pdo()->lastInsertId($name);
66186606
}
66196607

6620-
public function query($query, /* ?int */$fetchMode = null, ...$fetchModeArgs): \PDOStatement
6608+
public function query($query, /* ?int */ $fetchMode = null, ...$fetchModeArgs): \PDOStatement
66216609
{
66226610
return call_user_func_array(array($this->pdo(), 'query'), func_get_args());
66236611
}
@@ -9106,7 +9094,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
91069094
use Tqdev\PhpCrudApi\Controller\Responder;
91079095
use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
91089096
use Tqdev\PhpCrudApi\Middleware\Router\Router;
9109-
use Tqdev\PhpCrudApi\RequestUtils;
91109097
use Tqdev\PhpCrudApi\ResponseFactory;
91119098

91129099
class XmlMiddleware extends Middleware
@@ -9163,7 +9150,7 @@ private function json2xml($json, $types = 'null,boolean,number,string,object,arr
91639150
if ($t($v) == 'boolean') {
91649151
$va->appendChild($d->createTextNode($v ? 'true' : 'false'));
91659152
} else {
9166-
$va->appendChild($d->createTextNode($v));
9153+
$va->appendChild($d->createTextNode((string) $v));
91679154
}
91689155
$ch = $c->appendChild($va);
91699156
if (in_array($t($v), $ts)) {
@@ -9178,15 +9165,15 @@ private function json2xml($json, $types = 'null,boolean,number,string,object,arr
91789165
return $d->saveXML($d->documentElement);
91799166
}
91809167

9181-
private function xml2json($xml)
9168+
private function xml2json($xml): string
91829169
{
91839170
$o = @simplexml_load_string($xml);
9184-
if ($o===false) {
9185-
return null;
9171+
if ($o === false) {
9172+
return '';
91869173
}
91879174
$a = @dom_import_simplexml($o);
91889175
if (!$a) {
9189-
return null;
9176+
return '';
91909177
}
91919178
$t = function ($v) {
91929179
$t = $v->getAttribute('type');
@@ -9224,7 +9211,7 @@ private function xml2json($xml)
92249211
return $c;
92259212
};
92269213
$c = $f($f, $a);
9227-
return json_encode($c);
9214+
return (string) json_encode($c);
92289215
}
92299216

92309217
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface

src/Tqdev/PhpCrudApi/Database/LazyPdo.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(string $dsn, /*?string*/ $user = null, /*?string*/ $
2222
// explicitly NOT calling super::__construct
2323
}
2424

25-
public function addInitCommand(string $command)/*: void*/
25+
public function addInitCommand(string $command) /*: void*/
2626
{
2727
$this->commands[] = $command;
2828
}
@@ -107,17 +107,17 @@ public function prepare($statement, $options = array())
107107
return $this->pdo()->prepare($statement, $options);
108108
}
109109

110-
public function quote($string, $parameter_type = null): string
110+
public function quote($string, $parameter_type = \PDO::PARAM_STR): string
111111
{
112112
return $this->pdo()->quote($string, $parameter_type);
113113
}
114114

115-
public function lastInsertId(/* ?string */$name = null): string
115+
public function lastInsertId( /* ?string */$name = null): string
116116
{
117117
return $this->pdo()->lastInsertId($name);
118118
}
119119

120-
public function query($query, /* ?int */$fetchMode = null, ...$fetchModeArgs): \PDOStatement
120+
public function query($query, /* ?int */ $fetchMode = null, ...$fetchModeArgs): \PDOStatement
121121
{
122122
return call_user_func_array(array($this->pdo(), 'query'), func_get_args());
123123
}

src/Tqdev/PhpCrudApi/Middleware/XmlMiddleware.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Tqdev\PhpCrudApi\Controller\Responder;
1010
use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
1111
use Tqdev\PhpCrudApi\Middleware\Router\Router;
12-
use Tqdev\PhpCrudApi\RequestUtils;
1312
use Tqdev\PhpCrudApi\ResponseFactory;
1413

1514
class XmlMiddleware extends Middleware
@@ -66,7 +65,7 @@ private function json2xml($json, $types = 'null,boolean,number,string,object,arr
6665
if ($t($v) == 'boolean') {
6766
$va->appendChild($d->createTextNode($v ? 'true' : 'false'));
6867
} else {
69-
$va->appendChild($d->createTextNode($v));
68+
$va->appendChild($d->createTextNode((string) $v));
7069
}
7170
$ch = $c->appendChild($va);
7271
if (in_array($t($v), $ts)) {
@@ -81,15 +80,15 @@ private function json2xml($json, $types = 'null,boolean,number,string,object,arr
8180
return $d->saveXML($d->documentElement);
8281
}
8382

84-
private function xml2json($xml)
83+
private function xml2json($xml): string
8584
{
8685
$o = @simplexml_load_string($xml);
87-
if ($o===false) {
88-
return null;
86+
if ($o === false) {
87+
return '';
8988
}
9089
$a = @dom_import_simplexml($o);
9190
if (!$a) {
92-
return null;
91+
return '';
9392
}
9493
$t = function ($v) {
9594
$t = $v->getAttribute('type');
@@ -127,7 +126,7 @@ private function xml2json($xml)
127126
return $c;
128127
};
129128
$c = $f($f, $a);
130-
return json_encode($c);
129+
return (string) json_encode($c);
131130
}
132131

133132
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface

test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,5 @@ function run(array $drivers, string $dir, array $matches)
197197
}
198198
}
199199

200+
error_reporting(-1);
200201
run(['mysql', 'pgsql', 'sqlsrv', 'sqlite'], __DIR__ . '/tests', array_slice($argv, 1));

0 commit comments

Comments
 (0)