From 3eb1bb26a8e018a6fa7120d2962ef22958a96490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D1=80=D0=B0?= =?UTF-8?q?=D1=81=D0=B8=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2?= Date: Thu, 14 Jul 2016 17:55:23 +0300 Subject: [PATCH] Exception related changes. --- src/Document/Document.php | 2 +- src/EDM/Primitive.php | 25 +++++++++---------------- src/Element/Entry.php | 23 +++++++---------------- src/Element/Properties.php | 6 ++++-- src/ODataExtension.php | 12 +++++------- src/Service.php | 23 +++++++++++++++++------ src/URI/Filter.php | 4 ++-- src/URI/KeyPredicate.php | 1 + 8 files changed, 46 insertions(+), 50 deletions(-) diff --git a/src/Document/Document.php b/src/Document/Document.php index 715ce6c..61eb753 100644 --- a/src/Document/Document.php +++ b/src/Document/Document.php @@ -25,7 +25,7 @@ abstract class Document extends AtomPubDocument * @param Extensions $extensions Extension registry. * @param \DOMDocument|null $document Source document. * - * @throws \InvalidArgumentException + * @throws \InvalidArgumentException If $document root node has invalid name. * * @since 1.0 */ diff --git a/src/EDM/Primitive.php b/src/EDM/Primitive.php index 602c19b..1408705 100644 --- a/src/EDM/Primitive.php +++ b/src/EDM/Primitive.php @@ -48,9 +48,9 @@ class Primitive extends Element * @param \DOMElement|string $element DOM element or node name. * @param string|null $type Primitive type if $element is a string. * - * @since 1.0 + * @throws \InvalidArgumentException If $element has invalid namespace. * - * @throws \InvalidArgumentException + * @since 1.0 */ public function __construct(Node $parent, $element, $type = null) { @@ -61,6 +61,7 @@ public function __construct(Node $parent, $element, $type = null) $this->nodeName = (string) $element; parent::__construct($parent); if ($type) { + // Prefix "m" registered — no exception $this->setAttribute('m:type', $type); } } @@ -104,18 +105,12 @@ public function getName() * * @return string * - * @throws \InvalidArgumentException - * * @since 1.0 */ public function getType() { - return $this->getCachedProperty( - 'type', - function () { - return $this->getAttribute('m:type'); - } - ); + // Prefix "m" registered — no exception + return $this->getAttribute('m:type'); } /** @@ -123,14 +118,12 @@ function () { * * @param string $type * - * @throws \InvalidArgumentException - * * @since 1.0 */ public function setType($type) { + // Prefix "m" registered — no exception $this->setAttribute('m:type', $type); - $this->setCachedProperty('type', $type); } /** @@ -138,8 +131,6 @@ public function setType($type) * * @return mixed * - * @throws \InvalidArgumentException - * * @since 1.0 */ public function getValue() @@ -147,6 +138,7 @@ public function getValue() return $this->getCachedProperty( 'value', function () { + // Prefix "m" registered — no exception if ($this->getAttribute('m:null') === 'true') { return null; } @@ -198,7 +190,7 @@ function () { * * @param mixed $value * - * @throws \InvalidArgumentException + * @throws \InvalidArgumentException If the $value type does not match type set via setType() * * @since 1.0 */ @@ -206,6 +198,7 @@ public function setValue($value) { $element = $this->getDomElement(); if (null === $value) { + // Prefix "m" registered — no exception $this->setAttribute('m:null', 'true'); $element->nodeValue = ''; } diff --git a/src/Element/Entry.php b/src/Element/Entry.php index 5ed4a8c..44b0695 100644 --- a/src/Element/Entry.php +++ b/src/Element/Entry.php @@ -27,7 +27,6 @@ class Entry extends BaseEntry implements \ArrayAccess * * @return string|null * - * @throws \InvalidArgumentException * @throws \Mekras\Atom\Exception\MalformedNodeException * * @since 1.0 @@ -54,8 +53,7 @@ function () { * * @param string $type * - * @throws \InvalidArgumentException - * @throws \Mekras\Atom\Exception\MalformedNodeException + * @throws \Mekras\OData\Client\Exception\LogicException * * @since 1.0 */ @@ -71,7 +69,11 @@ public function setEntityType($type) } } - $this->addCategory($type)->setScheme(OData::SCHEME); + try { + $this->addCategory($type)->setScheme(OData::SCHEME); + } catch (\InvalidArgumentException $e) { + throw new LogicException($e->getMessage(), $e->getCode(), $e); + } } /** @@ -79,9 +81,6 @@ public function setEntityType($type) * * @return Properties * - * @throws \InvalidArgumentException - * @throws \Mekras\Atom\Exception\MalformedNodeException - * * @since 1.0 */ public function getProperties() @@ -89,6 +88,7 @@ public function getProperties() return $this->getCachedProperty( 'properties', function () { + // No REQUIRED — no exception $node = $this->query('atom:content/m:properties', self::SINGLE); if (null === $node) { return $this->getExtensions() @@ -169,9 +169,6 @@ public function addRelation($resource, $type = null) * @param string $offset An offset to check for. * * @return boolean true on success or false on failure. - * - * @throws \InvalidArgumentException - * @throws \Mekras\Atom\Exception\MalformedNodeException */ public function offsetExists($offset) { @@ -184,9 +181,6 @@ public function offsetExists($offset) * @param string $offset The offset to retrieve. * * @return Primitive - * - * @throws \InvalidArgumentException - * @throws \Mekras\Atom\Exception\MalformedNodeException */ public function offsetGet($offset) { @@ -200,7 +194,6 @@ public function offsetGet($offset) * @param mixed $value The value to set. * * @throws \InvalidArgumentException - * @throws \Mekras\Atom\Exception\MalformedNodeException * @throws \Mekras\OData\Client\Exception\LogicException */ public function offsetSet($offset, $value) @@ -223,8 +216,6 @@ public function offsetSet($offset, $value) * * @param string $offset The offset to unset. * - * @throws \InvalidArgumentException - * @throws \Mekras\Atom\Exception\MalformedNodeException * @throws \Mekras\OData\Client\Exception\LogicException * * @SuppressWarnings(PMD.UnusedFormalParameter) diff --git a/src/Element/Properties.php b/src/Element/Properties.php index 7b28125..c48c811 100644 --- a/src/Element/Properties.php +++ b/src/Element/Properties.php @@ -34,17 +34,19 @@ class Properties extends Element implements \Iterator * * @since 1.0 * - * @throws \InvalidArgumentException + * @throws \InvalidArgumentException If $element has invalid namespace. */ public function __construct(Content $parent, $element = null) { parent::__construct($parent, $element); if (null === $element) { + // No prefix — no exception $parent->setAttribute('type', 'application/xml'); } /** @var \DOMNodeList $nodes */ + // No REQUIRED — no exception $nodes = $this->query('d:*'); foreach ($nodes as $node) { $primitive = new Primitive($this, $node); @@ -60,8 +62,8 @@ public function __construct(Content $parent, $element = null) * @param string $type Primitive type (default is Primitive::STRING). * * @return Primitive - * @throws \InvalidArgumentException * + * @throws \InvalidArgumentException * @throws \Mekras\OData\Client\Exception\LogicException If property already exist. * * @since 1.0 diff --git a/src/ODataExtension.php b/src/ODataExtension.php index c0991c3..fc2537e 100644 --- a/src/ODataExtension.php +++ b/src/ODataExtension.php @@ -33,8 +33,6 @@ class ODataExtension implements DocumentExtension, ElementExtension, NamespaceEx * * @return Document|null * - * @throws \InvalidArgumentException - * * @since 1.0 */ public function parseDocument(Extensions $extensions, \DOMDocument $document) @@ -42,6 +40,7 @@ public function parseDocument(Extensions $extensions, \DOMDocument $document) if (OData::META === $document->documentElement->namespaceURI) { switch ($document->documentElement->localName) { case 'error': + // Node name already checked return new ErrorDocument($extensions, $document); } } @@ -57,8 +56,6 @@ public function parseDocument(Extensions $extensions, \DOMDocument $document) * * @return Document|null * - * @throws \InvalidArgumentException - * * @since 1.0 */ public function createDocument(Extensions $extensions, $name) @@ -74,8 +71,6 @@ public function createDocument(Extensions $extensions, $name) * * @return Element|null * - * @throws \InvalidArgumentException - * * @since 1.0 */ public function parseElement(Node $parent, \DOMElement $element) @@ -83,12 +78,14 @@ public function parseElement(Node $parent, \DOMElement $element) if (Atom::NS === $element->namespaceURI) { switch ($element->localName) { case 'entry': + // Node name already checked return new Entry($parent, $element); } } elseif (OData::META === $element->namespaceURI) { switch ($element->localName) { case 'properties': /** @var Content $parent */ + // Node name already checked return new Properties($parent, $element); } } @@ -103,7 +100,8 @@ public function parseElement(Node $parent, \DOMElement $element) * @param string $name Element name. * * @return Element|null - * @throws \InvalidArgumentException + * + * @throws \InvalidArgumentException If $element has invalid namespace. * * @since 1.0 */ diff --git a/src/Service.php b/src/Service.php index 351266d..784bce8 100644 --- a/src/Service.php +++ b/src/Service.php @@ -15,6 +15,7 @@ use Mekras\OData\Client\Document\ErrorDocument; use Mekras\OData\Client\Element\Entry; use Mekras\OData\Client\Exception\ClientErrorException; +use Mekras\OData\Client\Exception\LogicException; use Mekras\OData\Client\Exception\RuntimeException; use Mekras\OData\Client\Exception\ServerErrorException; use Psr\Http\Message\ResponseInterface; @@ -87,7 +88,8 @@ public function __construct( * * @return Document * - * @throws \InvalidArgumentException + * @throws \InvalidArgumentException If given document is not supported. + * @throws \Mekras\Atom\Exception\RuntimeException In case of XML errors. * @throws \Mekras\OData\Client\Exception\ClientErrorException * @throws \Mekras\OData\Client\Exception\RuntimeException * @throws \Mekras\OData\Client\Exception\ServerErrorException @@ -140,18 +142,27 @@ public function sendRequest($method, $uri, Document $document = null) * * @return EntryDocument * - * @throws \InvalidArgumentException - * @throws \Mekras\Atom\Exception\MalformedNodeException + * @throws \Mekras\OData\Client\Exception\LogicException * * @since 1.0 */ public function createEntityDocument($type) { - /** @var EntryDocument $document */ - $document = $this->documentFactory->createDocument('atom:entry'); + try { + $document = $this->documentFactory->createDocument('atom:entry'); + } catch (\InvalidArgumentException $e) { + throw new LogicException('Can not create entry document', 0, $e); + } + + if (!$document instanceof EntryDocument) { + throw new LogicException('Unexpected document type: ' . get_class($document)); + } - /** @var Entry $entry */ $entry = $document->getEntry(); + if (!$entry instanceof Entry) { + throw new LogicException('Unexpected entry type: ' . get_class($entry)); + } + $entry->setEntityType($type); $entry->addAuthor(''); $entry->addContent(''); diff --git a/src/URI/Filter.php b/src/URI/Filter.php index 3427b9c..480778f 100644 --- a/src/URI/Filter.php +++ b/src/URI/Filter.php @@ -97,7 +97,7 @@ public static function lte($arg1, $arg2) /** * Logical and. * - * @param string ...$args + * @param array|string ...$args * * @return string */ @@ -109,7 +109,7 @@ public static function lAnd(...$args) /** * Logical or. * - * @param string ...$args + * @param array|string ...$args * * @return string */ diff --git a/src/URI/KeyPredicate.php b/src/URI/KeyPredicate.php index 6a676d9..8c47cf4 100644 --- a/src/URI/KeyPredicate.php +++ b/src/URI/KeyPredicate.php @@ -44,6 +44,7 @@ public function __toString() { if (is_array($this->keyValue)) { $string = []; + /** @noinspection ForeachSourceInspection */ foreach ($this->keyValue as $key => $value) { $string[] = $key . '=' . $value; }