Skip to content

Commit

Permalink
declare types (#217)
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Staab <m.staab@complex-it.de>
  • Loading branch information
staabm and clxmstaab committed Jul 26, 2022
1 parent 0082dea commit f041084
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 76 deletions.
6 changes: 6 additions & 0 deletions lib/ContextStackTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ trait ContextStackTrait
* Values may also be a callable. In that case the function will be called
* directly.
*
* @phpstan-var array<string, class-string|callable|object>
*
* @var array
*/
public $elementMap = [];
Expand All @@ -55,6 +57,8 @@ trait ContextStackTrait
* You must make sure you create this entire list before starting to write.
* They should be registered on the root element.
*
* @phpstan-var array<string, class-string|string|null>
*
* @var array
*/
public $namespaceMap = [];
Expand All @@ -75,6 +79,8 @@ trait ContextStackTrait
*
* function (Writer $writer, object $value)
*
* @phpstan-var array<class-string, callable(Writer, object):mixed>
*
* @var array
*/
public $classMap = [];
Expand Down
8 changes: 6 additions & 2 deletions lib/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public function getClark()
*
* This function will also disable the standard libxml error handler (which
* usually just results in PHP errors), and throw exceptions instead.
*
* @return array{name: string, value: mixed, attributes: array}
*/
public function parse(): array
{
Expand Down Expand Up @@ -102,6 +104,8 @@ public function parse(): array
*
* If the $elementMap argument is specified, the existing elementMap will
* be overridden while parsing the tree, and restored after this process.
*
* @return array{name: string, value: mixed, attributes: array}|array{}
*/
public function parseGetElements(array $elementMap = null): array
{
Expand Down Expand Up @@ -297,9 +301,9 @@ public function getDeserializerForElementName(string $name): callable
}

$type = gettype($deserializer);
if ('string' === $type) {
if (is_string($deserializer)) {
$type .= ' ('.$deserializer.')';
} elseif ('object' === $type) {
} elseif (is_object($deserializer)) {
$type .= ' ('.get_class($deserializer).')';
}
throw new \LogicException('Could not use this type as a deserializer: '.$type.' for element: '.$name);
Expand Down
8 changes: 8 additions & 0 deletions lib/Serializer/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
* <s:elem5 attr="val" />
*
* @param string[] $values
*
* @return void
*/
function enum(Writer $writer, array $values)
{
Expand All @@ -55,6 +57,8 @@ function enum(Writer $writer, array $values)
* serialize empty properties, you must specify them as an empty string.
*
* @param object $valueObject
*
* @return void
*/
function valueObject(Writer $writer, $valueObject, string $namespace)
{
Expand Down Expand Up @@ -85,6 +89,8 @@ function valueObject(Writer $writer, $valueObject, string $namespace)
* and this could be called like this:
*
* repeatingElements($writer, $items, '{}item');
*
* @return void
*/
function repeatingElements(Writer $writer, array $items, string $childElementName)
{
Expand Down Expand Up @@ -149,6 +155,8 @@ function repeatingElements(Writer $writer, array $items, string $childElementNam
* You can even mix the two array syntaxes.
*
* @param string|int|float|bool|array|object $value
*
* @return void
*/
function standardSerializer(Writer $writer, $value)
{
Expand Down
16 changes: 12 additions & 4 deletions lib/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class Service
* Values may also be a callable. In that case the function will be called
* directly.
*
* @var array<string, class-string|callable>
* @phpstan-var array<string, class-string|callable|object>
*
* @var array
*/
public $elementMap = [];

Expand All @@ -36,7 +38,9 @@ class Service
* You must make sure you create this entire list before starting to write.
* They should be registered on the root element.
*
* @var array<string, class-string>
* @phpstan-var array<string, class-string|string|null>
*
* @var array
*/
public $namespaceMap = [];

Expand All @@ -56,7 +60,9 @@ class Service
*
* function (Writer $writer, object $value)
*
* @var array<class-string, callable(Writer,object):mixed>
* @phpstan-var array<class-string, callable(Writer, object):mixed>
*
* @var array
*/
public $classMap = [];

Expand Down Expand Up @@ -252,7 +258,7 @@ public function mapValueObject(string $elementName, string $className)
return \Sabre\Xml\Deserializer\valueObject($reader, $className, $namespace);
};
$this->classMap[$className] = function (Writer $writer, $valueObject) use ($namespace) {
return \Sabre\Xml\Serializer\valueObject($writer, $valueObject, $namespace);
\Sabre\Xml\Serializer\valueObject($writer, $valueObject, $namespace);
};
$this->valueObjectMap[$className] = $elementName;
}
Expand All @@ -269,6 +275,8 @@ public function mapValueObject(string $elementName, string $className)
* @param object $object
*
* @throws \InvalidArgumentException
*
* @return string
*/
public function writeValueObject($object, string $contextUri = null)
{
Expand Down
7 changes: 7 additions & 0 deletions lib/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class Writer extends XMLWriter
* ]
*
* @param mixed $value
*
* @return void
*/
public function write($value)
{
Expand Down Expand Up @@ -181,6 +183,7 @@ public function startElement($name): bool
* Note: this function doesn't have the string typehint, because PHP's
* XMLWriter::startElement doesn't either.
*
* @param string $name
* @param array|string|object|null $content
*/
public function writeElement($name, $content = null): bool
Expand All @@ -202,6 +205,10 @@ public function writeElement($name, $content = null): bool
* The key is an attribute name. If the key is a 'localName', the current
* xml namespace is assumed. If it's a 'clark notation key', this namespace
* will be used instead.
*
* @param array<string, string> $attributes
*
* @return void
*/
public function writeAttributes(array $attributes)
{
Expand Down
70 changes: 0 additions & 70 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -75,56 +75,21 @@ parameters:
count: 1
path: lib/Reader.php

-
message: "#^Property Sabre\\\\Xml\\\\Reader\\:\\:\\$classMap type has no value type specified in iterable type array\\.$#"
count: 1
path: lib/Reader.php

-
message: "#^Property Sabre\\\\Xml\\\\Reader\\:\\:\\$contextStack type has no value type specified in iterable type array\\.$#"
count: 1
path: lib/Reader.php

-
message: "#^Property Sabre\\\\Xml\\\\Reader\\:\\:\\$elementMap type has no value type specified in iterable type array\\.$#"
count: 1
path: lib/Reader.php

-
message: "#^Property Sabre\\\\Xml\\\\Reader\\:\\:\\$namespaceMap type has no value type specified in iterable type array\\.$#"
count: 1
path: lib/Reader.php

-
message: "#^Function Sabre\\\\Xml\\\\Serializer\\\\enum\\(\\) has no return type specified\\.$#"
count: 1
path: lib/Serializer/functions.php

-
message: "#^Function Sabre\\\\Xml\\\\Serializer\\\\repeatingElements\\(\\) has no return type specified\\.$#"
count: 1
path: lib/Serializer/functions.php

-
message: "#^Function Sabre\\\\Xml\\\\Serializer\\\\repeatingElements\\(\\) has parameter \\$items with no value type specified in iterable type array\\.$#"
count: 1
path: lib/Serializer/functions.php

-
message: "#^Function Sabre\\\\Xml\\\\Serializer\\\\standardSerializer\\(\\) has no return type specified\\.$#"
count: 1
path: lib/Serializer/functions.php

-
message: "#^Function Sabre\\\\Xml\\\\Serializer\\\\standardSerializer\\(\\) has parameter \\$value with no value type specified in iterable type array\\.$#"
count: 1
path: lib/Serializer/functions.php

-
message: "#^Function Sabre\\\\Xml\\\\Serializer\\\\valueObject\\(\\) has no return type specified\\.$#"
count: 1
path: lib/Serializer/functions.php

-
message: "#^Method Sabre\\\\Xml\\\\Service\\:\\:expect\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
Expand All @@ -140,26 +105,6 @@ parameters:
count: 1
path: lib/Service.php

-
message: "#^Method Sabre\\\\Xml\\\\Service\\:\\:writeValueObject\\(\\) has no return type specified\\.$#"
count: 1
path: lib/Service.php

-
message: "#^Method Sabre\\\\Xml\\\\Writer\\:\\:write\\(\\) has no return type specified\\.$#"
count: 1
path: lib/Writer.php

-
message: "#^Method Sabre\\\\Xml\\\\Writer\\:\\:writeAttributes\\(\\) has no return type specified\\.$#"
count: 1
path: lib/Writer.php

-
message: "#^Method Sabre\\\\Xml\\\\Writer\\:\\:writeAttributes\\(\\) has parameter \\$attributes with no value type specified in iterable type array\\.$#"
count: 1
path: lib/Writer.php

-
message: "#^Method Sabre\\\\Xml\\\\Writer\\:\\:writeElement\\(\\) has parameter \\$content with no value type specified in iterable type array\\.$#"
count: 1
Expand All @@ -175,26 +120,11 @@ parameters:
count: 1
path: lib/Writer.php

-
message: "#^Property Sabre\\\\Xml\\\\Writer\\:\\:\\$classMap type has no value type specified in iterable type array\\.$#"
count: 1
path: lib/Writer.php

-
message: "#^Property Sabre\\\\Xml\\\\Writer\\:\\:\\$contextStack type has no value type specified in iterable type array\\.$#"
count: 1
path: lib/Writer.php

-
message: "#^Property Sabre\\\\Xml\\\\Writer\\:\\:\\$elementMap type has no value type specified in iterable type array\\.$#"
count: 1
path: lib/Writer.php

-
message: "#^Property Sabre\\\\Xml\\\\Writer\\:\\:\\$namespaceMap type has no value type specified in iterable type array\\.$#"
count: 1
path: lib/Writer.php

-
message: "#^Method Sabre\\\\Xml\\\\ContextStackTest\\:\\:testPushAndPull\\(\\) has no return type specified\\.$#"
count: 1
Expand Down

0 comments on commit f041084

Please sign in to comment.