Skip to content

Commit

Permalink
Add typehints.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Aug 29, 2020
1 parent 65c8f31 commit 7758c95
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 122 deletions.
219 changes: 104 additions & 115 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@
}
}

/**
* Convenience function to check on "empty()"
*
* @param mixed $var
* @return bool Result
*/

if (!function_exists('isEmpty')) {
function isEmpty($var = null) {
/**
* Convenience function to check on "empty()"
*
* @param mixed $var
* @return bool Result
*/
function isEmpty($var = null): bool {
if (empty($var)) {
return true;
}
Expand All @@ -82,16 +81,15 @@ function isEmpty($var = null) {
}
}

/**
* Returns of what type the specific value is
*
* //TODO: use Debugger::exportVar() instead?
*
* @param mixed $value
* @return mixed Type (NULL, array, bool, float, int, string, object, unknown) + value
*/

if (!function_exists('returns')) {
/**
* Returns of what type the specific value is
*
* //TODO: use Debugger::exportVar() instead?
*
* @param mixed $value
* @return mixed Type (NULL, array, bool, float, int, string, object, unknown) + value
*/
function returns($value) {
if ($value === null) {
return 'NULL';
Expand Down Expand Up @@ -123,33 +121,31 @@ function returns($value) {
}
}

/**
* Returns htmlentities - string
*
* ENT_COMPAT = Will convert double-quotes and leave single-quotes alone.
* ENT_QUOTES = Will convert both double and single quotes. !!!
* ENT_NOQUOTES = Will leave both double and single quotes unconverted.
*
* @param string $text
* @return string Converted text
*/

if (!function_exists('ent')) {
function ent($text) {
return !empty($text) ? htmlentities($text, ENT_QUOTES, 'UTF-8') : '';
/**
* Returns htmlentities - string
*
* ENT_COMPAT = Will convert double-quotes and leave single-quotes alone.
* ENT_QUOTES = Will convert both double and single quotes. !!!
* ENT_NOQUOTES = Will leave both double and single quotes unconverted.
*
* @param string $text
* @return string Converted text
*/
function ent(string $text): string {
return htmlentities($text, ENT_QUOTES, 'UTF-8');
}
}

/**
* Convenience method for htmlspecialchars_decode
*
* @param string $text Text to wrap through htmlspecialchars_decode
* @param int $quoteStyle
* @return string Converted text
*/

if (!function_exists('hDec')) {
function hDec($text, $quoteStyle = ENT_QUOTES) {
/**
* Convenience method for htmlspecialchars_decode
*
* @param string $text Text to wrap through htmlspecialchars_decode
* @param int $quoteStyle
* @return string Converted text
*/
function hDec(string $text, int $quoteStyle = ENT_QUOTES): string {
if (is_array($text)) {
return array_map('hDec', $text);
}
Expand All @@ -158,16 +154,15 @@ function hDec($text, $quoteStyle = ENT_QUOTES) {
}
}

/**
* Convenience method for html_entity_decode
*
* @param string $text Text to wrap through htmlspecialchars_decode
* @param int $quoteStyle
* @return string Converted text
*/

if (!function_exists('entDec')) {
function entDec($text, $quoteStyle = ENT_QUOTES) {
/**
* Convenience method for html_entity_decode
*
* @param string $text Text to wrap through htmlspecialchars_decode
* @param int $quoteStyle
* @return string Converted text
*/
function entDec(string $text, int $quoteStyle = ENT_QUOTES): string {
if (is_array($text)) {
return array_map('entDec', $text);
}
Expand All @@ -176,18 +171,17 @@ function entDec($text, $quoteStyle = ENT_QUOTES) {
}
}

/**
* Focus is on the filename (without path)
*
* @deprecated Use native method instead
*
* @param string $filename to check on
* @param string|null $type (extension/ext, filename/file, basename/base, dirname/dir)
* @return mixed
*/

if (!function_exists('extractFileInfo')) {
function extractFileInfo($filename, $type = null) {
/**
* Focus is on the filename (without path)
*
* @deprecated Use native method instead
*
* @param string $filename to check on
* @param string|null $type (extension/ext, filename/file, basename/base, dirname/dir)
* @return mixed
*/
function extractFileInfo(string $filename, ?string $type = null) {
$info = extractPathInfo($filename, $type);
if ($info) {
return $info;
Expand All @@ -213,21 +207,20 @@ function extractFileInfo($filename, $type = null) {
}
}

/**
* Uses native PHP function to retrieve infos about a filename etc.
* Improves it by not returning non-file-name characters from url files if specified.
* So "filename.ext?foo=bar#hash" would simply be "filename.ext" then.
*
* @deprecated Use native method instead
*
* @param string $filename to check on
* @param string|null $type (extension/ext, filename/file, basename/base, dirname/dir)
* @param bool $fromUrl
* @return mixed
*/

if (!function_exists('extractPathInfo')) {
function extractPathInfo($filename, $type = null, $fromUrl = false) {
/**
* Uses native PHP function to retrieve infos about a filename etc.
* Improves it by not returning non-file-name characters from url files if specified.
* So "filename.ext?foo=bar#hash" would simply be "filename.ext" then.
*
* @deprecated Use native method instead
*
* @param string $filename to check on
* @param string|null $type (extension/ext, filename/file, basename/base, dirname/dir)
* @param bool $fromUrl
* @return mixed
*/
function extractPathInfo(string $filename, ?string $type = null, bool $fromUrl = false) {
switch ($type) {
case 'extension':
case 'ext':
Expand Down Expand Up @@ -266,19 +259,18 @@ function extractPathInfo($filename, $type = null, $fromUrl = false) {
}
}

/**
* Shows pr() messages, even with debug = 0.
* Also allows additional customization.
*
* @param mixed $var
* @param bool $collapsedAndExpandable
* @param array $options
* - class, showHtml, showFrom, jquery, returns, debug
* @return string HTML
*/

if (!function_exists('pre')) {
function pre($var, $collapsedAndExpandable = false, $options = []) {
/**
* Shows pr() messages, even with debug = 0.
* Also allows additional customization.
*
* @param mixed $var
* @param bool $collapsedAndExpandable
* @param array $options
* - class, showHtml, showFrom, jquery, returns, debug
* @return string HTML
*/
function pre($var, bool $collapsedAndExpandable = false, array $options = []): string {
$defaults = [
'class' => 'cake-debug',
'showHtml' => false, // Escape < and > (or manually escape with h() prior to calling this function)
Expand Down Expand Up @@ -328,34 +320,32 @@ function pre($var, $collapsedAndExpandable = false, $options = []) {
}
}

/**
* Checks if the string [$haystack] contains [$needle]
*
* @param string $haystack Input string.
* @param string $needle Needed char or string.
* @param bool $caseSensitive
* @return bool
*/

if (!function_exists('contains')) {
function contains($haystack, $needle, $caseSensitive = false) {
/**
* Checks if the string [$haystack] contains [$needle]
*
* @param string $haystack Input string.
* @param string $needle Needed char or string.
* @param bool $caseSensitive
* @return bool
*/
function contains(string $haystack, string $needle, bool $caseSensitive = false): bool {
$result = !$caseSensitive ? stripos($haystack, $needle) : strpos($haystack, $needle);

return $result !== false;
}
}

/**
* Checks if the string [$haystack] starts with [$needle]
*
* @param string $haystack Input string.
* @param string $needle Needed char or string.
* @param bool $caseSensitive
* @return bool
*/

if (!function_exists('startsWith')) {
function startsWith($haystack, $needle, $caseSensitive = false) {
/**
* Checks if the string [$haystack] starts with [$needle]
*
* @param string $haystack Input string.
* @param string $needle Needed char or string.
* @param bool $caseSensitive
* @return bool
*/
function startsWith(string $haystack, string $needle, bool $caseSensitive = false): bool {
if ($caseSensitive) {
return mb_strpos($haystack, $needle) === 0;
}
Expand All @@ -364,17 +354,16 @@ function startsWith($haystack, $needle, $caseSensitive = false) {
}
}

/**
* Checks if the String [$haystack] ends with [$needle]
*
* @param string $haystack Input string.
* @param string $needle Needed char or string
* @param bool $caseSensitive
* @return bool
*/

if (!function_exists('endsWith')) {
function endsWith($haystack, $needle, $caseSensitive = false) {
/**
* Checks if the String [$haystack] ends with [$needle]
*
* @param string $haystack Input string.
* @param string $needle Needed char or string
* @param bool $caseSensitive
* @return bool
*/
function endsWith(string $haystack, string $needle, bool $caseSensitive = false): bool {
if ($caseSensitive) {
return mb_strrpos($haystack, $needle) === mb_strlen($haystack) - mb_strlen($needle);
}
Expand Down
16 changes: 9 additions & 7 deletions tests/TestCase/Utility/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,15 +554,17 @@ public function testMonthNames() {
* @return void
*/
public function testRelLengthOfTime() {
$ret = $this->Time->relLengthOfTime('1990-11-20');
//pr($ret);
$result = $this->Time->relLengthOfTime('1990-11-20');
$this->assertTrue(!empty($result));
$this->assertTrue(is_string($result));

$ret = $this->Time->relLengthOfTime('2012-11-20');
//pr($ret);
$result = $this->Time->relLengthOfTime('2012-11-20');
$this->assertTrue(!empty($result));
$this->assertTrue(is_string($result));

$res = $this->Time->relLengthOfTime(date(FORMAT_DB_DATETIME, time() - 3600));
//pr($res);
$this->assertTrue(!empty($res));
$result = $this->Time->relLengthOfTime(date(FORMAT_DB_DATETIME, time() - 3600));
$this->assertTrue(!empty($result));
$this->assertTrue(is_string($result));

$res = $this->Time->relLengthOfTime(date(FORMAT_DB_DATETIME, time() - 4 * DAY - 5 * HOUR), null, ['plural' => 'n']);
//pr($res);
Expand Down

0 comments on commit 7758c95

Please sign in to comment.