Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

[*.neon]
indent_style = space
indent_size = 4
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: CI

on:
push:
pull_request:
types:
- synchronize
- opened

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Fix git issue
run: git config --global --add safe.directory /app
- name: Composer install
uses: php-actions/composer@v6
- name: Check code with phpstan
run: vendor/bin/phpstan analyze
60 changes: 31 additions & 29 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
{
"name": "papertower/wp-rest-api-psr7",
"description": "Provides PSR-7 and WP REST API Response and Request classes",
"type": "library",
"license": "MIT",
"require": {
"psr/http-message": "^1.0.1"
},
"repositories": [
{
"type": "git",
"url": "git@github.com:papertower/WP-REST-API-PSR7.git"
}
],
"keywords": [
"wp rest api",
"wordpress",
"psr-7"
],
"authors": [
{
"name": "Jason Adams",
"email": "jason@papertower.com"
}
],
"autoload": {
"psr-4": {
"WPRestApi\\PSR7\\": "src/"
}
}
"name": "papertower/wp-rest-api-psr7",
"description": "Provides PSR-7 and WP REST API Response and Request classes",
"type": "library",
"license": "MIT",
"require": {
"psr/http-message": "^2.0",
"league/uri": "^7.5"
},
"keywords": ["wp rest api", "wordpress", "psr-7"],
"authors": [
{
"name": "Jason Adams",
"email": "jason@papertower.com"
}
],
"autoload": {
"psr-4": {
"WPRestApi\\PSR7\\": "src/"
}
},
"require-dev": {
"php-stubs/wordpress-stubs": "^6.8",
"phpstan/phpstan": "^2.1",
"szepeviktor/phpstan-wordpress": "^2.0",
"phpstan/extension-installer": "^1.4"
},
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
}
}
}
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: 6
paths:
- src
ignoreErrors:
- identifier: missingType.iterableValue
- identifier: throws.unusedType
- identifier: missingType.generics
115 changes: 85 additions & 30 deletions src/BodyStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,52 @@

class BodyStream implements StreamInterface
{
private $stream;
private $size;
private $seekable;
private $readable;
private $writable;
private $uri;
private $customMetadata;
private mixed $stream;
private mixed $size;
private bool $seekable;
private bool $readable;
private bool $writable;
private mixed $uri;
private mixed $customMetadata;

/** @var array Hash of readable and writable stream types */
private static $readWriteHash = [
'read' => [
'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true,
'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true,
'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true,
'x+t' => true, 'c+t' => true, 'a+' => true
'r' => true,
'w+' => true,
'r+' => true,
'x+' => true,
'c+' => true,
'rb' => true,
'w+b' => true,
'r+b' => true,
'x+b' => true,
'c+b' => true,
'rt' => true,
'w+t' => true,
'r+t' => true,
'x+t' => true,
'c+t' => true,
'a+' => true
],
'write' => [
'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true,
'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true,
'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true,
'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true
'w' => true,
'w+' => true,
'rw' => true,
'r+' => true,
'x+' => true,
'c+' => true,
'wb' => true,
'w+b' => true,
'r+b' => true,
'x+b' => true,
'c+b' => true,
'w+t' => true,
'r+t' => true,
'x+t' => true,
'c+t' => true,
'a' => true,
'a+' => true
]
];

Expand Down Expand Up @@ -68,7 +93,7 @@ public function __construct($stream, $options = [])
$this->uri = $this->getMetadata('uri');
}

public function __get($name)
public function __get(string $name): void
{
if ($name == 'stream') {
throw new \RuntimeException('The stream is detached');
Expand All @@ -95,7 +120,7 @@ public function __toString()
}
}

public function getContents()
public function getContents(): string
{
$contents = stream_get_contents($this->stream);

Expand All @@ -106,7 +131,7 @@ public function getContents()
return $contents;
}

public function close()
public function close(): void
{
if (isset($this->stream)) {
if (is_resource($this->stream)) {
Expand All @@ -130,7 +155,10 @@ public function detach()
return $result;
}

public function getSize()
/**
* {@inheritDoc}
*/
public function getSize(): ?int
{
if ($this->size !== null) {
return $this->size;
Expand All @@ -154,27 +182,42 @@ public function getSize()
return null;
}

public function isReadable()
/**
* {@inheritDoc}
*/
public function isReadable(): bool
{
return $this->readable;
}

public function isWritable()
/**
* {@inheritDoc}
*/
public function isWritable(): bool
{
return $this->writable;
}

public function isSeekable()
/**
* {@inheritDoc}
*/
public function isSeekable(): bool
{
return $this->seekable;
}

public function eof()
/**
* {@inheritDoc}
*/
public function eof(): bool
{
return !$this->stream || feof($this->stream);
}

public function tell()
/**
* {@inheritDoc}
*/
public function tell(): int
{
$result = ftell($this->stream);

Expand All @@ -185,22 +228,31 @@ public function tell()
return $result;
}

public function rewind()
/**
* {@inheritDoc}
*/
public function rewind(): void
{
$this->seek(0);
}

public function seek($offset, $whence = SEEK_SET)
/**
* {@inheritDoc}
*/
public function seek($offset, $whence = SEEK_SET): void
{
if (!$this->seekable) {
throw new \RuntimeException('Stream is not seekable');
} elseif (fseek($this->stream, $offset, $whence) === -1) {
throw new \RuntimeException('Unable to seek to stream position '
. $offset . ' with whence ' . var_export($whence, true));
. $offset . ' with whence ' . var_export($whence, true));
}
}

public function read($length)
/**
* {@inheritDoc}
*/
public function read($length): string
{
if (!$this->readable) {
throw new \RuntimeException('Cannot read from non-readable stream');
Expand All @@ -221,7 +273,10 @@ public function read($length)
return $string;
}

public function write($string)
/**
* {@inheritDoc}
*/
public function write($string): int
{
if (!$this->writable) {
throw new \RuntimeException('Cannot write to a non-writable stream');
Expand Down Expand Up @@ -252,4 +307,4 @@ public function getMetadata($key = null)

return isset($meta[$key]) ? $meta[$key] : null;
}
}
}
Loading