Skip to content
Permalink
Browse files

Reduce redundant message wrapping code

Refers to #10925
  • Loading branch information...
ravage84 committed Jul 21, 2017
1 parent 24fc1d7 commit 718b42e15fb75c03a9e2904723e4f8daddaa220e
Showing with 24 additions and 24 deletions.
  1. +24 −24 src/Console/Shell.php
@@ -665,13 +665,8 @@ public function out($message = null, $newlines = 1, $level = Shell::NORMAL)
*/
public function err($message = null, $newlines = 1)
{
if (is_array($message)) {
foreach ($message as $k => $v) {
$message[$k] = '<error>' . $v . '</error>';
}
} else {
$message = '<error>' . $message . '</error>';
}
$messageType = 'error';
$message = $this->wrapMessageWithType($messageType, $message);
return $this->_io->err($message, $newlines);
}
@@ -687,13 +682,8 @@ public function err($message = null, $newlines = 1)
*/
public function info($message = null, $newlines = 1, $level = Shell::NORMAL)
{
if (is_array($message)) {
foreach ($message as $k => $v) {
$message[$k] = '<info>' . $v . '</info>';
}
} else {
$message = '<info>' . $message . '</info>';
}
$messageType = 'info';
$message = $this->wrapMessageWithType($messageType, $message);
return $this->out($message, $newlines, $level);
}
@@ -708,13 +698,8 @@ public function info($message = null, $newlines = 1, $level = Shell::NORMAL)
*/
public function warn($message = null, $newlines = 1)
{
if (is_array($message)) {
foreach ($message as $k => $v) {
$message[$k] = '<warning>' . $v . '</warning>';
}
} else {
$message = '<warning>' . $message . '</warning>';
}
$messageType = 'warning';
$message = $this->wrapMessageWithType($messageType, $message);
return $this->_io->err($message, $newlines);
}
@@ -729,16 +714,31 @@ public function warn($message = null, $newlines = 1)
* @see https://book.cakephp.org/3.0/en/console-and-shells.html#Shell::out
*/
public function success($message = null, $newlines = 1, $level = Shell::NORMAL)
{
$messageType = 'success';
$message = $this->wrapMessageWithType($messageType, $message);
return $this->out($message, $newlines, $level);
}
/**
* Wraps a message with a given message type, e.g. <warning>
*
* @param string $messageType The message type, e.g. "warning".
* @param string|array $message The message to wrap.
* @return array|string The message wrapped with the given message type.
*/
protected function wrapMessageWithType($messageType, $message)
{
if (is_array($message)) {
foreach ($message as $k => $v) {
$message[$k] = '<success>' . $v . '</success>';
$message[$k] = "<$messageType>" . $v . "</$messageType>";
}
} else {
$message = '<success>' . $message . '</success>';
$message = "<$messageType>" . $message . "</$messageType>";
}
return $this->out($message, $newlines, $level);
return $message;
}
/**

0 comments on commit 718b42e

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.