Code quality improvements

This commit is contained in:
Matias Griese 2019-10-08 16:58:59 +03:00
parent b641b0e442
commit 565f26db8e
23 changed files with 129 additions and 115 deletions

View File

@ -319,7 +319,7 @@ class Cache extends Getters
*
* @param string $id the id of the cached entry
*
* @return object|bool returns the cached entry, can be any type, or false if doesn't exist
* @return mixed|bool returns the cached entry, can be any type, or false if doesn't exist
*/
public function fetch($id)
{

View File

@ -40,23 +40,23 @@ class Errors
$error_page->setPageTitle('Crikey! There was an error...');
$error_page->addResourcePath(GRAV_ROOT . '/system/assets');
$error_page->addCustomCss('whoops.css');
$whoops->pushHandler($error_page);
$whoops->prependHandler($error_page);
break;
case -1:
$whoops->pushHandler(new BareHandler);
$whoops->prependHandler(new BareHandler);
break;
default:
$whoops->pushHandler(new SimplePageHandler);
$whoops->prependHandler(new SimplePageHandler);
break;
}
if (Whoops\Util\Misc::isAjaxRequest() || $jsonRequest) {
$whoops->pushHandler(new Whoops\Handler\JsonResponseHandler);
$whoops->prependHandler(new Whoops\Handler\JsonResponseHandler);
}
if (isset($config['log']) && $config['log']) {
$logger = $grav['log'];
$whoops->pushHandler(function($exception, $inspector, $run) use ($logger) {
$whoops->prependHandler(function($exception, $inspector, $run) use ($logger) {
try {
$logger->addCritical($exception->getMessage() . ' - Trace: ' . $exception->getTraceAsString());
} catch (\Exception $e) {

View File

@ -67,7 +67,7 @@ trait MediaTrait
/**
* Gets the associated media collection.
*
* @return MediaCollectionInterface Representation of associated media.
* @return MediaCollectionInterface|Media Representation of associated media.
*/
public function getMedia()
{
@ -89,7 +89,7 @@ trait MediaTrait
/**
* Sets the associated media collection.
*
* @param MediaCollectionInterface $media Representation of associated media.
* @param MediaCollectionInterface|Media $media Representation of associated media.
* @return $this
*/
protected function setMedia(MediaCollectionInterface $media)

View File

@ -253,6 +253,13 @@ interface PageCollectionInterface extends \Traversable, \ArrayAccess, \Countable
*/
public function ofOneOfTheseAccessLevels($accessLevels);
/**
* Converts collection into an array.
*
* @return array
*/
public function toArray();
/**
* Get the extended version of this Collection with each page keyed by route
*

View File

@ -9,7 +9,7 @@
namespace Grav\Common\Page\Interfaces;
use Grav\Common\Page\Media;
use Grav\Common\Media\Interfaces\MediaCollectionInterface;
/**
* Methods currently implemented in Flex Page emulation layer.
@ -20,7 +20,6 @@ interface PageContentInterface
* Gets and Sets the header based on the YAML configuration at the top of the .md file
*
* @param object|array $var a YAML object representing the configuration for the file
*
* @return object the current YAML configuration
*/
public function header($var = null);
@ -28,10 +27,8 @@ interface PageContentInterface
/**
* Get the summary.
*
* @param int $size Max summary size.
*
* @param int|null $size Max summary size.
* @param bool $textOnly Only count text size.
*
* @return string
*/
public function summary($size = null, $textOnly = false);
@ -46,8 +43,7 @@ interface PageContentInterface
/**
* Gets and Sets the content based on content portion of the .md file
*
* @param string $var Content
*
* @param string|null $var Content
* @return string Content
*/
public function content($var = null);
@ -70,7 +66,6 @@ interface PageContentInterface
* Gets and Sets the Page raw content
*
* @param string|null $var
*
* @return string
*/
public function rawMarkdown($var = null);
@ -79,7 +74,7 @@ interface PageContentInterface
* Get value from a page variable (used mostly for creating edit forms).
*
* @param string $name Variable name.
* @param mixed $default
* @param mixed|null $default
*
* @return mixed
*/
@ -88,18 +83,16 @@ interface PageContentInterface
/**
* Gets and sets the associated media as found in the page folder.
*
* @param Media $var Representation of associated media.
*
* @return Media Representation of associated media.
* @param MediaCollectionInterface|null $var New media object.
* @return MediaCollectionInterface Representation of associated media.
*/
public function media($var = null);
/**
* Gets and sets the title for this Page. If no title is set, it will use the slug() to get a name
*
* @param string $var the title of the Page
*
* @return string the title of the Page
* @param string|null $var New title of the Page
* @return string The title of the Page
*/
public function title($var = null);
@ -107,45 +100,40 @@ interface PageContentInterface
* Gets and sets the menu name for this Page. This is the text that can be used specifically for navigation.
* If no menu field is set, it will use the title()
*
* @param string $var the menu field for the page
*
* @return string the menu field for the page
* @param string|null $var New menu field for the page
* @return string The menu field for the page
*/
public function menu($var = null);
/**
* Gets and Sets whether or not this Page is visible for navigation
*
* @param bool $var true if the page is visible
*
* @return bool true if the page is visible
* @param bool|null $var New value
* @return bool True if the page is visible
*/
public function visible($var = null);
/**
* Gets and Sets whether or not this Page is considered published
*
* @param bool $var true if the page is published
*
* @return bool true if the page is published
* @param bool|null $var New value
* @return bool True if the page is published
*/
public function published($var = null);
/**
* Gets and Sets the Page publish date
*
* @param string $var string representation of a date
*
* @return int unix timestamp representation of the date
* @param string|null $var String representation of the new date
* @return int Unix timestamp representation of the date
*/
public function publishDate($var = null);
/**
* Gets and Sets the Page unpublish date
*
* @param string $var string representation of a date
*
* @return int|null unix timestamp representation of the date
* @param string|null $var String representation of the new date
* @return int|null Unix timestamp representation of the date
*/
public function unpublishDate($var = null);
@ -153,9 +141,8 @@ interface PageContentInterface
* Gets and Sets the process setup for this Page. This is multi-dimensional array that consists of
* a simple array of arrays with the form array("markdown"=>true) for example
*
* @param array $var an Array of name value pairs where the name is the process and value is true or false
*
* @return array an Array of name value pairs where the name is the process and value is true or false
* @param array|null $var New array of name value pairs where the name is the process and value is true or false
* @return array Array of name value pairs where the name is the process and value is true or false
*/
public function process($var = null);
@ -163,63 +150,56 @@ interface PageContentInterface
* Gets and Sets the slug for the Page. The slug is used in the URL routing. If not set it uses
* the parent folder from the path
*
* @param string $var the slug, e.g. 'my-blog'
*
* @return string the slug
* @param string|null $var New slug, e.g. 'my-blog'
* @return string The slug
*/
public function slug($var = null);
/**
* Get/set order number of this page.
*
* @param int $var
*
* @return string|bool
* @param int|null $var New order as a number
* @return string|bool Order in a form of '02.' or false if not set
*/
public function order($var = null);
/**
* Gets and sets the identifier for this Page object.
*
* @param string $var the identifier
*
* @return string the identifier
* @param string|null $var New identifier
* @return string The identifier
*/
public function id($var = null);
/**
* Gets and sets the modified timestamp.
*
* @param int $var modified unix timestamp
*
* @return int modified unix timestamp
* @param int|null $var New modified unix timestamp
* @return int Modified unix timestamp
*/
public function modified($var = null);
/**
* Gets and sets the option to show the last_modified header for the page.
*
* @param boolean $var show last_modified header
*
* @return boolean show last_modified header
* @param bool|null $var New last_modified header value
* @return bool Show last_modified header
*/
public function lastModified($var = null);
/**
* Get/set the folder.
*
* @param string $var Optional path
*
* @return string|null
* @param string|null $var New folder
* @return string|null The folder
*/
public function folder($var = null);
/**
* Gets and sets the date for this Page object. This is typically passed in via the page headers
*
* @param string $var string representation of a date
*
* @return int unix timestamp representation of the date
* @param string|null $var New string representation of a date
* @return int Unix timestamp representation of the date
*/
public function date($var = null);
@ -227,27 +207,24 @@ interface PageContentInterface
* Gets and sets the date format for this Page object. This is typically passed in via the page headers
* using typical PHP date string structure - http://php.net/manual/en/function.date.php
*
* @param string $var string representation of a date format
*
* @return string string representation of a date format
* @param string|null $var New string representation of a date format
* @return string String representation of a date format
*/
public function dateformat($var = null);
/**
* Gets and sets the taxonomy array which defines which taxonomies this page identifies itself with.
*
* @param array $var an array of taxonomies
*
* @return array an array of taxonomies
* @param array|null $var New array of taxonomies
* @return array An array of taxonomies
*/
public function taxonomy($var = null);
/**
* Gets the configured state of the processing method.
*
* @param string $process the process, eg "twig" or "markdown"
*
* @return bool whether or not the processing method is enabled for this Page
* @param string $process The process name, eg "twig" or "markdown"
* @return bool Whether or not the processing method is enabled for this Page
*/
public function shouldProcess($process);

View File

@ -16,7 +16,6 @@ interface PageFormInterface
* Add forms to this page.
*
* @param array $new
* @param bool $override
* @return $this
*/
public function addForms(array $new/*, $override = true*/);

View File

@ -1212,8 +1212,7 @@ class Page implements PageInterface
* Gets and sets the associated media as found in the page folder.
*
* @param Media $var Representation of associated media.
*
* @return MediaCollectionInterface|Media Representation of associated media.
* @return Media Representation of associated media.
*/
public function media($var = null)
{

View File

@ -517,7 +517,7 @@ class Pages
}
/**
* @param $value
* @param array|string $value
* @param PageInterface|null $self
* @return Collection
*/
@ -702,11 +702,8 @@ class Pages
$instance = $this->instances[(string)$path] ?? null;
if (\is_string($instance)) {
$instance = $this->flex ? $this->flex->getObject($instance) : null;
if ($instance) {
$instance = $instance->hasTranslation() ? $instance->getTranslation() : $instance;
if (method_exists($instance, 'initialize') && $this->grav['config']->get('system.pages.events.page')) {
$instance->initialize();
}
if (method_exists($instance, 'initialize') && $this->grav['config']->get('system.pages.events.page')) {
$instance->initialize();
}
}
if ($instance && !$instance instanceof PageInterface) {
@ -1329,6 +1326,15 @@ class Pages
* @var PageInterface|FlexObjectInterface $page
*/
foreach ($collection as $key => $page) {
$path = $page->path();
// FIXME: We really need to do better than this.
$page = $page->hasTranslation() ? $page->getTranslation() : null;
if (!$page || $path === $root_path) {
continue;
}
if ($config->get('system.pages.events.page')) {
if (method_exists($page, 'initialize')) {
$page->initialize();
@ -1338,19 +1344,11 @@ class Pages
}
}
$path = $page->path();
// FIXME: We really need to do better than this.
$translated = $page->hasTranslation() ? $page->getTranslation() : false;
if (!$translated || $path === $root_path) {
continue;
}
$parent = dirname($path);
$this->instances[$path] = $page->getFlexKey();
// FIXME: ... better...
$this->children[$parent][$path] = ['slug' => $translated->slug()];
$this->children[$parent][$path] = ['slug' => $page->slug()];
if (!isset($this->children[$path])) {
$this->children[$path] = [];
}
@ -1372,7 +1370,7 @@ class Pages
$this->buildRoutes();
// cache if needed
if (isset($cache)) {
if (null !== $cache) {
/** @var Taxonomy $taxonomy */
$taxonomy = $this->grav['taxonomy'];
$taxonomy_map = $taxonomy->taxonomy();

View File

@ -18,7 +18,7 @@ class Security
/**
* Sanitize SVG for XSS code
*
* @param $file
* @param string $file
*/
public static function sanitizeSVG($file)
{

View File

@ -15,7 +15,7 @@ use Grav\Common\Data\Data;
use Grav\Common\File\CompiledYamlFile;
use Grav\Common\Grav;
use Grav\Common\Page\Media;
use Grav\Common\Page\Medium\ImageMedium;
use Grav\Common\Page\Medium\Medium;
use Grav\Common\Page\Medium\MediumFactory;
use Grav\Common\User\Authentication;
use Grav\Common\User\Interfaces\UserInterface;

View File

@ -544,8 +544,12 @@ class User extends FlexObject implements UserInterface, MediaManipulationInterfa
return \count($this->jsonSerialize());
}
/**
* @return MediaCollectionInterface
*/
public function getMedia()
{
/** @var Media $media */
$media = $this->getFlexMedia();
// Deal with shared avatar folder.

View File

@ -56,6 +56,7 @@ abstract class Utils
/** @var Uri $uri */
$uri = $grav['uri'];
$resource = false;
if (static::contains((string)$input, '://')) {
/** @var UniformResourceLocator $locator */
$locator = $grav['locator'];
@ -331,7 +332,7 @@ abstract class Utils
* @param array $a
* @return array|false
*/
public static function arrayLower(Array $a)
public static function arrayLower(array $a)
{
return array_map('mb_strtolower', $a);
}
@ -339,11 +340,11 @@ abstract class Utils
/**
* Simple function to remove item/s in an array by value
*
* @param $search array
* @param $value string|array
* @param array $search
* @param string|array $value
* @return array
*/
public static function arrayRemoveValue(Array $search, $value)
public static function arrayRemoveValue(array $search, $value)
{
foreach ((array) $value as $val) {
$key = array_search($val, $search);

View File

@ -121,6 +121,9 @@ class PageSystemValidatorCommand extends ConsoleCommand
//'getAction' => [[]],
];
/** @var Grav */
protected $grav;
protected function configure()
{
$this

View File

@ -27,7 +27,9 @@ class ServerCommand extends ConsoleCommand
const SYMFONY_SERVER = 'Symfony Server';
const PHP_SERVER = 'Built-in PHP Server';
/** @var string */
protected $ip;
/** @var int */
protected $port;
protected $io;
@ -60,16 +62,16 @@ class ServerCommand extends ConsoleCommand
$php = $executableFinder->find(false);
$this->ip = '127.0.0.1';
$this->port = intval($this->input->getOption('port') ?? 8000);
$this->port = (int)($this->input->getOption('port') ?? 8000);
// Get an open port
while (!$this->portAvailable($this->ip, $this->port)) {
$this->port += 1;
$this->port++;
}
// Setup the commands
$symfony_cmd = ['symfony', 'server:start', '--ansi', '--port=' . $this->port ];
$symfony_cmd = ['symfony', 'server:start', '--ansi', '--port=' . $this->port];
$php_cmd = [$php, '-S', $this->ip.':'.$this->port, 'system/router.php'];
$commands = [
@ -107,7 +109,7 @@ class ServerCommand extends ConsoleCommand
$process->start();
if ($name === self::PHP_SERVER) {
$this->io->success('Built-in PHP web server listening on http://' . $this->ip . ':' . $this->port . ' (PHP v' . phpversion() . ')');
$this->io->success('Built-in PHP web server listening on http://' . $this->ip . ':' . $this->port . ' (PHP v' . PHP_VERSION . ')');
}
$process->wait(function ($type, $buffer) {
@ -120,18 +122,18 @@ class ServerCommand extends ConsoleCommand
/**
* Simple function test the port
*
* @param $ip
* @param $port
* @param string $ip
* @param int $port
* @return bool
*/
protected function portAvailable($ip, $port) {
protected function portAvailable($ip, $port)
{
$fp = @fsockopen($ip, $port, $errno, $errstr, 0.1);
if (!$fp) {
return true;
} else {
fclose($fp);
return false;
}
}
fclose($fp);
return false;
}
}

View File

@ -197,7 +197,7 @@ class AbstractFileCollection extends AbstractLazyCollection implements FileColle
}
/**
* @param \RecursiveDirectoryIterator[] $children
* @param array $children
* @return array
*/
protected function doInitializeChildren(array $children, $nestingLimit)

View File

@ -31,6 +31,7 @@ abstract class AbstractLazyCollection extends BaseAbstractLazyCollection impleme
public function reverse()
{
$this->initialize();
return $this->collection->reverse();
}
@ -40,6 +41,7 @@ abstract class AbstractLazyCollection extends BaseAbstractLazyCollection impleme
public function shuffle()
{
$this->initialize();
return $this->collection->shuffle();
}
@ -49,6 +51,7 @@ abstract class AbstractLazyCollection extends BaseAbstractLazyCollection impleme
public function chunk($size)
{
$this->initialize();
return $this->collection->chunk($size);
}
@ -58,6 +61,7 @@ abstract class AbstractLazyCollection extends BaseAbstractLazyCollection impleme
public function select(array $keys)
{
$this->initialize();
return $this->collection->select($keys);
}
@ -67,6 +71,7 @@ abstract class AbstractLazyCollection extends BaseAbstractLazyCollection impleme
public function unselect(array $keys)
{
$this->initialize();
return $this->collection->unselect($keys);
}
@ -76,6 +81,7 @@ abstract class AbstractLazyCollection extends BaseAbstractLazyCollection impleme
public function jsonSerialize()
{
$this->initialize();
return $this->collection->jsonSerialize();
}
}

View File

@ -621,7 +621,9 @@ class FlexDirectory implements FlexAuthorizeInterface
}
}
$fetch && $debugger->stopTimer('flex-objects');
if ($fetch) {
$debugger->stopTimer('flex-objects');
}
return $list;
}

View File

@ -34,7 +34,7 @@ class FlexFormFlash extends FormFlash
$serialized = parent::jsonSerialize();
$object = $this->getObject();
if ($object) {
if ($object instanceof FlexObjectInterface) {
$serialized['object'] = [
'type' => $object->getFlexType(),
'key' => $object->getKey() ?: null,

View File

@ -121,4 +121,12 @@ interface FlexCollectionInterface extends FlexCommonInterface, ObjectCollectionI
* @return FlexIndexInterface Returns a Flex Index from the current collection.
*/
public function getIndex();
/**
* Get metadata associated to the object
*
* @param string $key Key.
* @return array
*/
public function getMetaData(string $key): array;
}

View File

@ -55,6 +55,13 @@ interface FlexObjectInterface extends FlexCommonInterface, NestedObjectInterface
*/
public function search(string $search, $properties = null, array $options = null): float;
/**
* Returns true if object has a key.
*
* @return bool
*/
public function hasKey();
/**
* Get a unique key for the object.
*

View File

@ -380,7 +380,7 @@ class FolderStorage extends AbstractFilesystemStorage
$locator->clearCache();
}
} catch (\RuntimeException $e) {
throw new \RuntimeException(sprintf('Flex saveFile(%s): %s', $file->filename(), $e->getMessage()));
throw new \RuntimeException(sprintf('Flex saveFile(%s): %s', $path ?? $key, $e->getMessage()));
}
$row['__META'] = $this->getObjectMeta($key, true);

View File

@ -30,7 +30,7 @@ class FormFlash implements FormFlashInterface
protected $formName;
/** @var string */
protected $url;
/** @var array */
/** @var array|null */
protected $user;
/** @var int */
protected $createdTimestamp;
@ -221,7 +221,7 @@ class FormFlash implements FormFlashInterface
/**
* @inheritDoc
*/
public function save(bool $force = false): self
public function save(bool $force = false)
{
if (!($this->folder && $this->uniqueId)) {
return $this;
@ -243,7 +243,7 @@ class FormFlash implements FormFlashInterface
/**
* @inheritDoc
*/
public function delete(): self
public function delete()
{
if ($this->folder && $this->uniqueId) {
$this->removeTmpDir();

View File

@ -19,6 +19,7 @@ use Grav\Common\Twig\Twig;
use Grav\Common\User\Interfaces\UserInterface;
use Grav\Common\Utils;
use Grav\Framework\ContentBlock\HtmlBlock;
use Grav\Framework\Form\Interfaces\FormFlashInterface;
use Grav\Framework\Form\Interfaces\FormInterface;
use Grav\Framework\Session\SessionInterface;
use Psr\Http\Message\ServerRequestInterface;
@ -54,7 +55,7 @@ trait FormTrait
private $data;
/** @var array|UploadedFileInterface[] */
private $files;
/** @var FormFlash|null */
/** @var FormFlashInterface|null */
private $flash;
/** @var string */
private $flashFolder;