Skip to content

Commit

Permalink
fix: fix passing null as non-nullable function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
marcantondahmen committed Apr 2, 2022
1 parent d9f3382 commit 3822317
Show file tree
Hide file tree
Showing 24 changed files with 170 additions and 150 deletions.
4 changes: 2 additions & 2 deletions automad/src/Core/Blocks.php
Expand Up @@ -81,11 +81,11 @@ public static function render(string $json, Automad $Automad) {
$html = '';

if (!is_object($data)) {
return false;
return $html;
}

if (!isset($data->blocks)) {
return false;
return $html;
}

$data = self::prepareData($data);
Expand Down
37 changes: 19 additions & 18 deletions automad/src/Core/Page.php
Expand Up @@ -121,28 +121,29 @@ public function __get(string $key) {
* @return string The requested value
*/
public function get(string $key) {
// Check whether the requested data is part of the data array or has to be generated.
// Return value from the data array.
if (array_key_exists($key, $this->data)) {
// Return value from the data array.
return $this->data[$key];
} elseif (array_key_exists($key, $this->Shared->data)) {
// Return value from the Shared data array.
}

// Return value from the Shared data array.
if (array_key_exists($key, $this->Shared->data)) {
return $this->Shared->data[$key];
} else {
// Generate system variable value or return false.
switch ($key) {
case AM_KEY_CURRENT_PAGE:
return $this->isCurrent();
case AM_KEY_CURRENT_PATH:
return $this->isInCurrentPath();
case AM_KEY_BASENAME:
return basename($this->path);
case AM_KEY_MTIME:
return $this->getMtime();
default:
return false;
}
}

// Generate system variable value or return an empty string.
switch ($key) {
case AM_KEY_CURRENT_PAGE:
return $this->isCurrent();
case AM_KEY_CURRENT_PATH:
return $this->isInCurrentPath();
case AM_KEY_BASENAME:
return basename($this->path);
case AM_KEY_MTIME:
return $this->getMtime();
default:
return '';
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion automad/src/Core/SessionData.php
Expand Up @@ -61,7 +61,7 @@ public static function get(?string $key = null) {
if (array_key_exists($key, $_SESSION['data'])) {
return $_SESSION['data'][$key];
} else {
return false;
return '';
}
} else {
return $_SESSION['data'];
Expand Down
140 changes: 76 additions & 64 deletions automad/src/Core/Str.php
Expand Up @@ -60,39 +60,41 @@ class Str {
* the strftime() syntax has to be used.
* Multiple values can be passed as a CSV string for the locale parameter.
*
* @param string|null $date
* @param string|null $format
* @param string $date
* @param string $format
* @param string|null $locale
* @return string The formatted date
*/
public static function dateFormat(?string $date, ?string $format = null, ?string $locale = null) {
if ($date) {
if (strpos($format, '%') !== false) {
$original = setlocale(LC_TIME, 0);

if ($locale) {
setlocale(LC_TIME, Parse::csv($locale));
}

$formatted = strftime($format, strtotime($date));
setlocale(LC_TIME, $original);
} else {
$formatted = date($format, strtotime($date));
public static function dateFormat(string $date, string $format = '', ?string $locale = null) {
if (!$date || !$format) {
return '';
}

if (strpos($format, '%') !== false) {
$original = setlocale(LC_TIME, 0);

if ($locale) {
setlocale(LC_TIME, Parse::csv($locale));
}

$formatted = strftime($format, strtotime($date));
setlocale(LC_TIME, $original);

return $formatted;
}

return date($format, strtotime($date));
}

/**
* Set a default value for $str in case $str is empty.
*
* @param string|null $str
* @param string|null $defaultValue
* @param string $str
* @param string $defaultValue
* @return string The default value
*/
public static function def(?string $str, ?string $defaultValue = null) {
if ($str === null || trim($str) === '') {
public static function def(string $str, string $defaultValue = '') {
if (trim($str) === '') {
$str = $defaultValue;
}

Expand All @@ -102,14 +104,10 @@ public static function def(?string $str, ?string $defaultValue = null) {
/**
* Escapes a string to be used safely in a JSON string.
*
* @param string|null $str
* @param string $str
* @return string The escaped string
*/
public static function escape(?string $str) {
if ($str === null) {
return '';
}

public static function escape(string $str) {
// Escape values to be used in headless mode.
// The json_encode() function is used to create a valid JSON string
// with only one temporary key.
Expand All @@ -126,75 +124,87 @@ public static function escape(?string $str) {
/**
* Find the URL of the first image within rendered HTML markup.
*
* @param string|null $str
* @return string The URL of the first image or false
* @param string $str
* @return string The URL of the first image or an empty string
*/
public static function findFirstImage(?string $str) {
public static function findFirstImage(string $str) {
if (!$str) {
return '';
}

preg_match('/<img[^>]+src="([^"]+)"/is', $str, $matches);

if (!empty($matches[1])) {
return $matches[1];
}

return '';
}

/**
* Find the first paragraph in rendered HTML and return its inner HTML.
*
* @param string|null $str
* @return string The inner HTML of the first paragraph or false
* @param string $str
* @return string The inner HTML of the first paragraph or an empty string
*/
public static function findFirstParagraph(?string $str) {
public static function findFirstParagraph(string $str) {
if (!$str) {
return '';
}

// First remove any paragraph only containing an image.
$str = preg_replace('/<p>\s*<img.+?><\/p>/is', '', $str);
preg_match('/<p\b[^>]*>(.*?)<\/p>/is', $str, $matches);

if (!empty($matches[1])) {
return $matches[1];
}

return '';
}

/**
* Parse a markdown string. Optionally skip parsing in case $str is a single line string.
*
* @param string|null $str
* @param string $str
* @param bool $multilineOnly
* @return string The parsed string
*/
public static function markdown(?string $str, $multilineOnly = false) {
public static function markdown(string $str, $multilineOnly = false) {
// In case $str has no line breaks and $multilineOnly is enabled, skip parsing.
if (strpos($str, "\n") === false && $multilineOnly) {
return $str;
} else {
$str = MarkdownExtra::defaultTransform($str);
}

return preg_replace_callback('/\<h(2|3)\>(.*?)\<\/h\1\>/i', function ($matches) {
$id = self::sanitize(self::stripTags($matches[2]), true, 100);
$str = MarkdownExtra::defaultTransform($str);

return "<h{$matches[1]} id=\"$id\">{$matches[2]}</h{$matches[1]}>";
}, $str);
}
return preg_replace_callback('/\<h(2|3)\>(.*?)\<\/h\1\>/i', function ($matches) {
$id = self::sanitize(self::stripTags($matches[2]), true, 100);

return "<h{$matches[1]} id=\"$id\">{$matches[2]}</h{$matches[1]}>";
}, $str);
}

/**
* Perform a regex match.
*
* @param string|null $str
* @param string|null $regex
* @param string $str
* @param string $regex
* @return int 1 or 0
*/
public static function match(?string $str, ?string $regex = null) {
public static function match(string $str, string $regex = '') {
return preg_match($regex, $str);
}

/**
* Search and replace by regex.
*
* @param string|null $str
* @param string|null $regex
* @param string|null $replace
* @param string $str
* @param string $regex
* @param string $replace
* @return string The processed string
*/
public static function replace(?string $str, ?string $regex = null, ?string $replace = null) {
public static function replace(string $str, string $regex = '', string $replace = '') {
return preg_replace($regex, $replace, $str);
}

Expand All @@ -206,12 +216,12 @@ public static function replace(?string $str, ?string $regex = null, ?string $rep
* Note: To produce fully safe prefixes and directory names,
* possible dots should be removed by setting $removeDots = true.
*
* @param string|null $str
* @param string $str
* @param bool $removeDots
* @param int $maxChars
* @return string The sanitized string
*/
public static function sanitize(?string $str, $removeDots = false, $maxChars = 100) {
public static function sanitize(string $str, $removeDots = false, $maxChars = 100) {
if (strlen($str) === 0) {
return '';
}
Expand Down Expand Up @@ -243,12 +253,16 @@ public static function sanitize(?string $str, $removeDots = false, $maxChars = 1
/**
* Shortens a string keeping full words. Note that this method also first strips all tags from the given string.
*
* @param string|null $str
* @param string $str
* @param int $maxChars
* @param string $ellipsis
* @return string The shortened string
*/
public static function shorten(?string $str, $maxChars, string $ellipsis = ' ...') {
public static function shorten(string $str, $maxChars, string $ellipsis = ' ...') {
if (strlen($str) === 0) {
return '';
}

$str = Str::stripTags($str);
$str = preg_replace('/[\n\r]+/s', ' ', $str);

Expand All @@ -274,12 +288,12 @@ public static function shorten(?string $str, $maxChars, string $ellipsis = ' ...
* In case the sanitized string is empty or the string is shorter than 6 chars while the
* input string is longer than 12 chars, the string is replaced with a md5 hash shortened to 16 chars.
*
* @param string|null $str
* @param string $str
* @param bool $removeDots
* @param int $maxChars
* @return string the slug
*/
public static function slug(?string $str, $removeDots = false, $maxChars = 100) {
public static function slug(string $str, $removeDots = false, $maxChars = 100) {
if (strlen($str) === 0) {
return '';
}
Expand All @@ -296,34 +310,32 @@ public static function slug(?string $str, $removeDots = false, $maxChars = 100)
/**
* Strip substring from end of string.
*
* @param string|null $str
* @param string|null $end
* @param string $str
* @param string $end
* @return string The processed string
*/
public static function stripEnd(?string $str, ?string $end = null) {
public static function stripEnd(string $str, string $end = '') {
return preg_replace('/' . preg_quote($end, '/') . '$/', '', $str);
}

/**
* Strip substring from start of string.
*
* @param string|null $str
* @param string|null $start
* @param string $str
* @param string $start
* @return string The processed string
*/
public static function stripStart(?string $str, ?string $start = null) {
public static function stripStart(string $str, string $start = '') {
return preg_replace('/^' . preg_quote($start, '/') . '/', '', $str);
}

/**
* Removes all HTML and Markdown (!) tags.
*
* @param string|null $str
* @param string $str
* @return string The clean string
*/
public static function stripTags(?string $str) {
if ($str !== null) {
return trim(strip_tags(Str::markdown(strip_tags($str))));
}
public static function stripTags(string $str) {
return trim(strip_tags(Str::markdown(strip_tags($str))));
}
}
4 changes: 2 additions & 2 deletions automad/src/Engine/Pipe.php
Expand Up @@ -160,10 +160,10 @@ private static function math(string $operator, string $number, string $value) {
*
* @param string $function
* @param array $parameters
* @param string|null $value
* @param string $value
* @return string $value
*/
private static function stringFunction(string $function, array $parameters, ?string $value = null) {
private static function stringFunction(string $function, array $parameters, string $value = '') {
if (!$parameters) {
$parameters = array();
}
Expand Down

0 comments on commit 3822317

Please sign in to comment.