From 87f17d296d75e66fd43f24b708d738421ea2927c Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Tue, 15 Oct 2019 19:40:57 +0300 Subject: [PATCH] More phpstan issues --- system/src/Grav/Framework/Flex/FlexForm.php | 2 +- .../Flex/Pages/Traits/PageLegacyTrait.php | 7 +- .../Flex/Pages/Traits/PageRoutableTrait.php | 18 +- .../Framework/Flex/Traits/FlexMediaTrait.php | 6 +- .../Grav/Framework/Form/Traits/FormTrait.php | 14 +- .../Object/Base/ObjectCollectionTrait.php | 182 +++++++++++++++++- 6 files changed, 202 insertions(+), 27 deletions(-) diff --git a/system/src/Grav/Framework/Flex/FlexForm.php b/system/src/Grav/Framework/Flex/FlexForm.php index e7073127e..d07662ee9 100644 --- a/system/src/Grav/Framework/Flex/FlexForm.php +++ b/system/src/Grav/Framework/Flex/FlexForm.php @@ -252,7 +252,7 @@ class FlexForm implements FlexFormInterface /** * Get form flash object. * - * @return FlexFormFlash + * @return FormFlashInterface|FlexFormFlash */ public function getFlash() { diff --git a/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php b/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php index d9969d184..c9fb1d834 100644 --- a/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php +++ b/system/src/Grav/Framework/Flex/Pages/Traits/PageLegacyTrait.php @@ -297,11 +297,12 @@ trait PageLegacyTrait } else { $parent = $parentStorageKey ? $this->getFlexDirectory()->getObject($parentStorageKey, 'storage_key') - : $index->getRoot(); + : (method_exists($index, 'getRoot') ? $index->getRoot() : null); } // Find non-existing key. - $key = trim($parent->getKey() . '/' . basename($this->getKey()), '/'); + $parentKey = $parent ? $parent->getKey() : ''; + $key = trim($parentKey . '/' . basename($this->getKey()), '/'); $key = preg_replace('/-\d+$/', '', $key); $i = 1; do { @@ -913,7 +914,7 @@ trait PageLegacyTrait /** * Helper method to return an ancestor page. * - * @param bool|null $lookup Name of the parent folder + * @param string|null $lookup Name of the parent folder * * @return PageInterface|null page you were looking for if it exists */ diff --git a/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php b/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php index a95efe5b2..599d7c558 100644 --- a/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php +++ b/system/src/Grav/Framework/Flex/Pages/Traits/PageRoutableTrait.php @@ -48,7 +48,6 @@ trait PageRoutableTrait * The page must be *routable* and *published* * * @param bool $var true if the page is routable - * * @return bool true if the page is routable */ public function routable($var = null): bool @@ -68,7 +67,6 @@ trait PageRoutableTrait * Gets the URL for a page - alias of url(). * * @param bool $include_host - * * @return string the permalink */ public function link($include_host = false): string @@ -89,7 +87,6 @@ trait PageRoutableTrait * Returns the canonical URL for a page * * @param bool $include_lang - * * @return string */ public function canonical($include_lang = true): string @@ -104,7 +101,6 @@ trait PageRoutableTrait * @param bool $canonical true to return the canonical URL * @param bool $include_base * @param bool $raw_route - * * @return string The url. */ public function url($include_host = false, $canonical = false, $include_base = true, $raw_route = false): string @@ -156,7 +152,6 @@ trait PageRoutableTrait * the parents route and the current Page's slug. * * @param string $var Set new default route. - * * @return string|null The route for the Page. */ public function route($var = null): ?string @@ -183,7 +178,6 @@ trait PageRoutableTrait * Gets and Sets the page raw route * * @param string|null $var - * * @return string|null */ public function rawRoute($var = null): ?string @@ -201,7 +195,6 @@ trait PageRoutableTrait * Gets the route aliases for the page based on page headers. * * @param array $var list of route aliases - * * @return array The route aliases for the Page. */ public function routeAliases($var = null): array @@ -219,7 +212,6 @@ trait PageRoutableTrait * that value, else if it's `true` it will use the default route. * * @param string|null $var - * * @return string */ public function routeCanonical($var = null): string @@ -235,7 +227,6 @@ trait PageRoutableTrait * Gets the redirect set in the header. * * @param string $var redirect url - * * @return string|null */ public function redirect($var = null): ?string @@ -273,7 +264,6 @@ trait PageRoutableTrait * This is equivalent to the filePath but without the filename. * * @param string $var the path - * * @return string|null the path */ public function path($var = null): ?string @@ -302,7 +292,6 @@ trait PageRoutableTrait * Get/set the folder. * * @param string $var Optional path, including numeric prefix. - * * @return string|null */ public function folder($var = null): ?string @@ -324,7 +313,6 @@ trait PageRoutableTrait * Get/set the folder. * * @param string $var Optional path, including numeric prefix. - * * @return string|null */ public function parentStorageKey($var = null): ?string @@ -347,7 +335,6 @@ trait PageRoutableTrait * Gets and Sets the parent object for this page * * @param PageInterface $var the parent page object - * * @return PageInterface|null the parent page object if it exists. */ public function parent(PageInterface $var = null) @@ -365,7 +352,7 @@ trait PageRoutableTrait $index = $directory->getIndex(); - return $index->getRoot(); + return method_exists($index, 'getRoot') ? $index->getRoot() : null; } /** @@ -434,11 +421,12 @@ trait PageRoutableTrait if (isset($routes[$uri_path])) { /** @var PageInterface $child_page|null */ $child_page = $pages->dispatch($uri->route())->parent(); - if ($child_page) { + if (null !== $child_page) { while (!$child_page->root()) { if ($this->path() === $child_page->path()) { return true; } + /** @var PageInterface $child_page|null */ $child_page = $child_page->parent(); } } diff --git a/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php b/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php index 7a3d8d5e4..a58082646 100644 --- a/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php +++ b/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php @@ -69,9 +69,13 @@ trait FlexMediaTrait $media = $this->getExistingMedia(); // Include uploaded media to the object media. + /** + * @var string $filename + * @var UploadedFileInterface $upload + */ foreach ($this->getUpdatedMedia() as $filename => $upload) { // Just make sure we do not include removed or moved media. - if (null !== $upload && $upload->getError() === \UPLOAD_ERR_OK && !$upload->isMoved()) { + if (null !== $upload && $upload->getError() === \UPLOAD_ERR_OK && (!$upload instanceof FormFlashFile || !$upload->isMoved())) { $updated = true; $media->add($filename, MediumFactory::fromUploadedFile($upload)); } diff --git a/system/src/Grav/Framework/Form/Traits/FormTrait.php b/system/src/Grav/Framework/Form/Traits/FormTrait.php index 77860719b..4fa311e97 100644 --- a/system/src/Grav/Framework/Form/Traits/FormTrait.php +++ b/system/src/Grav/Framework/Form/Traits/FormTrait.php @@ -351,7 +351,7 @@ trait FormTrait /** * Get form flash object. * - * @return FormFlash + * @return FormFlashInterface */ public function getFlash() { @@ -375,7 +375,7 @@ trait FormTrait /** * Get all available form flash objects for this form. * - * @return FormFlash[] + * @return FormFlashInterface[] */ public function getAllFlashes(): array { @@ -460,9 +460,13 @@ trait FormTrait /** @var UserInterface|null $user */ $user = $grav['user'] ?? null; - $userExists = $user && $user->exists(); - $username = $userExists ? $user->username : null; - $mediaFolder = $userExists ? $user->getMediaFolder() : null; + if (null !== $user && $user->exists()) { + $username = $user->username; + $mediaFolder = $user->getMediaFolder(); + } else { + $username = null; + $mediaFolder = null; + } $session = $grav['session'] ?? null; $sessionId = $session ? $session->getId() : null; diff --git a/system/src/Grav/Framework/Object/Base/ObjectCollectionTrait.php b/system/src/Grav/Framework/Object/Base/ObjectCollectionTrait.php index e2ef0019f..60a50bc93 100644 --- a/system/src/Grav/Framework/Object/Base/ObjectCollectionTrait.php +++ b/system/src/Grav/Framework/Object/Base/ObjectCollectionTrait.php @@ -17,8 +17,183 @@ use Grav\Framework\Object\Interfaces\ObjectInterface; */ trait ObjectCollectionTrait { - use ObjectTrait { - setKey as public; + /** @var string */ + protected static $type; + + /** + * @var string + */ + private $_key; + + /** + * @return string + */ + protected function getTypePrefix() + { + return ''; + } + + /** + * @param bool $prefix + * @return string + */ + public function getType($prefix = true) + { + $type = $prefix ? $this->getTypePrefix() : ''; + + if (static::$type) { + return $type . static::$type; + } + + $class = \get_class($this); + return $type . strtolower(substr($class, strrpos($class, '\\') + 1)); + } + + /** + * @return string + */ + public function getKey() + { + return $this->_key ?: $this->getType() . '@@' . spl_object_hash($this); + } + + /** + * @return bool + */ + public function hasKey() + { + return !empty($this->_key); + } + + /** + * @param string $property Object property name. + * @return bool[] True if property has been defined (can be null). + */ + public function hasProperty($property) + { + return $this->doHasProperty($property); + } + + /** + * @param string $property Object property to be fetched. + * @param mixed $default Default value if property has not been set. + * @return mixed[] Property values. + */ + public function getProperty($property, $default = null) + { + return $this->doGetProperty($property, $default); + } + + /** + * @param string $property Object property to be updated. + * @param mixed $value New value. + * @return $this + */ + public function setProperty($property, $value) + { + $this->doSetProperty($property, $value); + + return $this; + } + + /** + * @param string $property Object property to be unset. + * @return $this + */ + public function unsetProperty($property) + { + $this->doUnsetProperty($property); + + return $this; + } + + /** + * @param string $property Object property to be defined. + * @param mixed $default Default value. + * @return $this + */ + public function defProperty($property, $default) + { + if (!$this->hasProperty($property)) { + $this->setProperty($property, $default); + } + + return $this; + } + + /** + * Implements Serializable interface. + * + * @return string + */ + public function serialize() + { + return serialize($this->doSerialize()); + } + + /** + * @param string $serialized + */ + public function unserialize($serialized) + { + $data = unserialize($serialized); + + if (method_exists($this, 'initObjectProperties')) { + $this->initObjectProperties(); + } + $this->doUnserialize($data); + } + + /** + * @return array + */ + protected function doSerialize() + { + return ['key' => $this->getKey(), 'type' => $this->getType(), 'elements' => $this->getElements()]; + } + + /** + * @param array $serialized + */ + protected function doUnserialize(array $serialized) + { + if (!isset($serialized['key'], $serialized['type'], $serialized['elements']) || $serialized['type'] !== $this->getType()) { + throw new \InvalidArgumentException("Cannot unserialize '{$this->getType()}': Bad data"); + } + + $this->setKey($serialized['key']); + $this->setElements($serialized['elements']); + } + + /** + * Implements JsonSerializable interface. + * + * @return array + */ + public function jsonSerialize() + { + return $this->doSerialize(); + } + + /** + * Returns a string representation of this object. + * + * @return string + */ + public function __toString() + { + return $this->getKey(); + } + + /** + * @param string $key + * @return $this + */ + public function setKey($key) + { + $this->_key = (string) $key; + + return $this; } /** @@ -182,4 +357,7 @@ trait ObjectCollectionTrait * @return \Traversable */ abstract public function getIterator(); + + abstract protected function getElements(); + abstract protected function setElements(array $elements); }