Skip to content

Commit

Permalink
Merge pull request #1572 from daniellienert/task/type-utility-classes
Browse files Browse the repository at this point in the history
TASK: Define types in the flow utility classes
  • Loading branch information
daniellienert committed May 13, 2019
2 parents a4484ef + 355e3a6 commit bca6158
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 26 deletions.
23 changes: 15 additions & 8 deletions Neos.Flow/Classes/Utility/Algorithms.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Neos\Flow\Utility;

/*
Expand All @@ -11,6 +13,7 @@
* source code.
*/

use Neos\Utility\Unicode\Functions;
use Ramsey\Uuid\Uuid;
use Neos\Flow\Annotations as Flow;

Expand All @@ -28,9 +31,10 @@ class Algorithms
* If php-uuid was installed it will be used instead to speed up the process.
*
* @return string The universally unique id
* @throws \Exception
* @todo Optionally generate type 1 and type 5 UUIDs.
*/
public static function generateUUID()
public static function generateUUID(): string
{
if (is_callable('uuid_create')) {
return strtolower(uuid_create(UUID_TYPE_RANDOM));
Expand All @@ -42,21 +46,23 @@ public static function generateUUID()
/**
* Returns a string of random bytes.
*
* @param integer $count Number of bytes to generate
* @param int $count Number of bytes to generate
* @return string Random bytes
* @throws \Exception
*/
public static function generateRandomBytes($count)
public static function generateRandomBytes(int $count): string
{
return random_bytes($count);
}

/**
* Returns a random token in hex format.
*
* @param integer $count Token length
* @param int $count Token length
* @return string A random token
* @throws \Exception
*/
public static function generateRandomToken($count)
public static function generateRandomToken(int $count): string
{
return bin2hex(random_bytes($count));
}
Expand All @@ -67,13 +73,14 @@ public static function generateRandomToken($count)
* @param integer $count Number of characters to generate
* @param string $characters Allowed characters, defaults to alpha-numeric (a-zA-Z0-9)
* @return string A random string
* @throws \Exception
*/
public static function generateRandomString($count, $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
public static function generateRandomString(int $count, string $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'): string
{
$characterCount = \Neos\Utility\Unicode\Functions::strlen($characters);
$characterCount = Functions::strlen($characters);
$string = '';
for ($i = 0; $i < $count; $i++) {
$string .= \Neos\Utility\Unicode\Functions::substr($characters, random_int(0, ($characterCount - 1)), 1);
$string .= Functions::substr($characters, random_int(0, ($characterCount - 1)), 1);
}

return $string;
Expand Down
24 changes: 15 additions & 9 deletions Neos.Flow/Classes/Utility/Environment.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Neos\Flow\Utility;

/*
Expand All @@ -17,6 +19,7 @@
use Neos\Flow\Error\Exception as ErrorException;
use Neos\Flow\Utility\Exception as UtilityException;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Utility\Exception\FilesException;
use Neos\Utility\Files;

/**
Expand All @@ -41,7 +44,7 @@ class Environment
* The base path of $temporaryDirectory. This property can (and should) be set from outside.
* @var string
*/
protected $temporaryDirectoryBase;
protected $temporaryDirectoryBase = '';

/**
* @var string
Expand All @@ -64,7 +67,7 @@ public function __construct(ApplicationContext $context)
* @param string $temporaryDirectoryBase Base path of the temporary directory, with trailing slash
* @return void
*/
public function setTemporaryDirectoryBase($temporaryDirectoryBase)
public function setTemporaryDirectoryBase(string $temporaryDirectoryBase): void
{
$this->temporaryDirectoryBase = $temporaryDirectoryBase;
$this->temporaryDirectory = null;
Expand All @@ -75,8 +78,10 @@ public function setTemporaryDirectoryBase($temporaryDirectoryBase)
*
* @return string Path to PHP's temporary directory
* @api
* @throws Exception
* @throws FilesException
*/
public function getPathToTemporaryDirectory()
public function getPathToTemporaryDirectory(): string
{
if ($this->temporaryDirectory !== null) {
return $this->temporaryDirectory;
Expand All @@ -92,19 +97,19 @@ public function getPathToTemporaryDirectory()
*
* @return integer The maximum available path length
*/
public function getMaximumPathLength()
public function getMaximumPathLength(): int
{
return PHP_MAXPATHLEN;
}

/**
* Whether or not URL rewriting is enabled.
*
* @return boolean
* @return bool
*/
public function isRewriteEnabled()
public function isRewriteEnabled(): bool
{
return (boolean)Bootstrap::getEnvironmentConfigurationSetting('FLOW_REWRITEURLS');
return (bool)Bootstrap::getEnvironmentConfigurationSetting('FLOW_REWRITEURLS');
}

/**
Expand All @@ -118,8 +123,9 @@ public function isRewriteEnabled()
* @param string $temporaryDirectoryBase Full path to the base for the temporary directory
* @return string The full path to the temporary directory
* @throws UtilityException if the temporary directory could not be created or is not writable
* @throws FilesException
*/
protected function createTemporaryDirectory($temporaryDirectoryBase)
protected function createTemporaryDirectory(string $temporaryDirectoryBase): string
{
$temporaryDirectoryBase = Files::getUnixStylePath($temporaryDirectoryBase);
if (substr($temporaryDirectoryBase, -1, 1) !== '/') {
Expand All @@ -145,7 +151,7 @@ protected function createTemporaryDirectory($temporaryDirectoryBase)
/**
* @return ApplicationContext
*/
public function getContext()
public function getContext(): ApplicationContext
{
return $this->context;
}
Expand Down
2 changes: 2 additions & 0 deletions Neos.Flow/Classes/Utility/Exception.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Neos\Flow\Utility;

/*
Expand Down
6 changes: 4 additions & 2 deletions Neos.Flow/Classes/Utility/Ip.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Neos\Flow\Utility;

/*
Expand Down Expand Up @@ -36,9 +38,9 @@ class Ip
*
* @param string $ip The IP to match
* @param string $range The CIDR range pattern to match against
* @return boolean true if the pattern matched, false otherwise
* @return bool true if the pattern matched, false otherwise
*/
public static function cidrMatch($ip, $range)
public static function cidrMatch(string $ip, string $range): bool
{
if (strpos($range, '/') === false) {
$bits = null;
Expand Down
2 changes: 2 additions & 0 deletions Neos.Flow/Classes/Utility/Now.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Neos\Flow\Utility;

/*
Expand Down
17 changes: 10 additions & 7 deletions Neos.Flow/Classes/Utility/PhpAnalyzer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Neos\Flow\Utility;

/*
Expand Down Expand Up @@ -35,17 +37,17 @@ class PhpAnalyzer
/**
* @param string $phpCode
*/
public function __construct($phpCode)
public function __construct(string $phpCode)
{
$this->phpCode = $phpCode;
}

/**
* Extracts the Fully Qualified Class name from the given PHP code
*
* @return string FQN in the format "Some\Fully\Qualified\ClassName" or NULL if no class was detected
* @return string|null FQN in the format "Some\Fully\Qualified\ClassName" or NULL if no class was detected
*/
public function extractFullyQualifiedClassName()
public function extractFullyQualifiedClassName(): ?string
{
$fullyQualifiedClassName = $this->extractClassName();
if ($fullyQualifiedClassName === null) {
Expand All @@ -55,15 +57,16 @@ public function extractFullyQualifiedClassName()
if ($namespace !== null) {
$fullyQualifiedClassName = $namespace . '\\' . $fullyQualifiedClassName;
}

return $fullyQualifiedClassName;
}

/**
* Extracts the PHP namespace from the given PHP code
*
* @return string the PHP namespace in the form "Some\Namespace" (w/o leading backslash) - or NULL if no namespace modifier was found
* @return string|null the PHP namespace in the form "Some\Namespace" (w/o leading backslash) - or NULL if no namespace modifier was found
*/
public function extractNamespace()
public function extractNamespace(): ?string
{
$namespaceParts = [];
$tokens = token_get_all($this->phpCode);
Expand Down Expand Up @@ -99,9 +102,9 @@ public function extractNamespace()
* Extracts the className of the given PHP code
* Note: This only returns the class name without namespace, @see extractFullyQualifiedClassName()
*
* @return string
* @return string|null
*/
public function extractClassName()
public function extractClassName(): ?string
{
$tokens = token_get_all($this->phpCode);
$numberOfTokens = count($tokens);
Expand Down

0 comments on commit bca6158

Please sign in to comment.