Skip to content

Commit

Permalink
Merge pull request #9717 from robbieaverill/pulls/4.7/improve-stuff
Browse files Browse the repository at this point in the history
Update spaces in syntax, single quotes, early returns where possible
  • Loading branch information
Sam Minnée committed Oct 2, 2020
2 parents 7184703 + 4d54a21 commit 4ab36ac
Show file tree
Hide file tree
Showing 30 changed files with 153 additions and 161 deletions.
3 changes: 1 addition & 2 deletions src/ORM/Connect/DBConnector.php
Expand Up @@ -122,9 +122,8 @@ protected function isQueryType($sql, $type)
$operation = $matches['operation'];
if (is_array($type)) {
return in_array(strtolower($operation), $type);
} else {
return strcasecmp($sql, $type) === 0;
}
return strcasecmp($sql, $type) === 0;
}

/**
Expand Down
52 changes: 23 additions & 29 deletions src/ORM/Connect/DBSchemaManager.php
Expand Up @@ -788,51 +788,45 @@ public function alterationMessage($message, $type = "")
if (!$this->supressOutput) {
if (Director::is_cli()) {
switch ($type) {
case "created":
case "changed":
case "repaired":
$sign = "+";
case 'created':
case 'changed':
case 'repaired':
$sign = '+';
break;
case "obsolete":
case "deleted":
case 'obsolete':
case 'deleted':
$sign = '-';
break;
case "notice":
case 'notice':
$sign = '*';
break;
case "error":
$sign = "!";
case 'error':
$sign = '!';
break;
default:
$sign = " ";
$sign = ' ';
}
$message = strip_tags($message);
echo " $sign $message\n";
} else {
switch ($type) {
case "created":
$class = "success";
case 'created':
$class = 'success';
break;
case "obsolete":
$class = "error";
case 'obsolete':
case 'error':
case 'deleted':
$class = 'error';
break;
case "notice":
$class = "warning";
case 'notice':
$class = 'warning';
break;
case "error":
$class = "error";
break;
case "deleted":
$class = "error";
break;
case "changed":
$class = "info";
break;
case "repaired":
$class = "info";
case 'changed':
case 'repaired':
$class = 'info';
break;
default:
$class = "";
$class = '';
}
echo "<li class=\"$class\">$message</li>";
}
Expand Down Expand Up @@ -878,7 +872,7 @@ public function fixTableCase($tableName)

$this->alterationMessage(
"Table $tableName: renamed from $currentName",
"repaired"
'repaired'
);

// Rename via temp table to avoid case-sensitivity issues
Expand Down
3 changes: 1 addition & 2 deletions src/ORM/Connect/Database.php
Expand Up @@ -254,9 +254,8 @@ protected function benchmarkQuery($sql, $callback, $parameters = [])
Backtrace::backtrace();
}
return $result;
} else {
return $callback($sql);
}
return $callback($sql);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Connect/DatabaseException.php
Expand Up @@ -53,7 +53,7 @@ public function getParameters()
* @param string $sql The SQL executed for this query
* @param array $parameters The parameters given for this query, if any
*/
function __construct($message = '', $code = 0, $previous = null, $sql = null, $parameters = [])
public function __construct($message = '', $code = 0, $previous = null, $sql = null, $parameters = [])
{
parent::__construct($message, $code, $previous);
$this->sql = $sql;
Expand Down
23 changes: 12 additions & 11 deletions src/ORM/FieldType/DBBoolean.php
Expand Up @@ -11,7 +11,6 @@
*/
class DBBoolean extends DBField
{

public function __construct($name = null, $defaultVal = 0)
{
$this->defaultVal = ($defaultVal) ? 1 : 0;
Expand All @@ -21,15 +20,15 @@ public function __construct($name = null, $defaultVal = 0)

public function requireField()
{
$parts=[
'datatype'=>'tinyint',
'precision'=>1,
'sign'=>'unsigned',
'null'=>'not null',
'default'=>$this->defaultVal,
'arrayValue'=>$this->arrayValue
$parts = [
'datatype' => 'tinyint',
'precision' => 1,
'sign' => 'unsigned',
'null' => 'not null',
'default' => $this->defaultVal,
'arrayValue' => $this->arrayValue
];
$values=['type'=>'boolean', 'parts'=>$parts];
$values = ['type' => 'boolean', 'parts' => $parts];
DB::require_field($this->tableName, $this->name, $values);
}

Expand Down Expand Up @@ -81,9 +80,11 @@ public function prepValueForDB($value)
{
if (is_bool($value)) {
return $value ? 1 : 0;
} elseif (empty($value)) {
}
if (empty($value)) {
return 0;
} elseif (is_string($value)) {
}
if (is_string($value)) {
switch (strtolower($value)) {
case 'false':
case 'f':
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/FieldType/DBClassName.php
Expand Up @@ -4,9 +4,9 @@

use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\Deprecation;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\Dev\Deprecation;

/**
* Represents a classname selector, which respects obsolete clasess.
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/FieldType/DBComposite.php
Expand Up @@ -126,7 +126,7 @@ public function compositeDatabaseFields()
public function isChanged()
{
// When unbound, use the local changed flag
if (! ($this->record instanceof DataObject)) {
if (!$this->record instanceof DataObject) {
return $this->isChanged;
}

Expand Down
1 change: 0 additions & 1 deletion src/ORM/FieldType/DBCurrency.php
Expand Up @@ -35,7 +35,6 @@ public function __construct($name = null, $wholeSize = 9, $decimalSize = 2, $def
*/
public function Nice()
{
// return "<span title=\"$this->value\">$" . number_format($this->value, 2) . '</span>';
$val = $this->config()->currency_symbol . number_format(abs($this->value), 2);
if ($this->value < 0) {
return "($val)";
Expand Down
56 changes: 30 additions & 26 deletions src/ORM/FieldType/DBDate.php
Expand Up @@ -335,11 +335,11 @@ public function RangeString($otherDateObj, $includeOrdinals = false)

if ($y1 != $y2) {
return "$d1 $m1 $y1 - $d2 $m2 $y2";
} elseif ($m1 != $m2) {
}
if ($m1 != $m2) {
return "$d1 $m1 - $d2 $m2 $y1";
} else {
return "$d1 - $d2 $m1 $y1";
}
return "$d1 - $d2 $m1 $y1";
}

/**
Expand Down Expand Up @@ -393,19 +393,18 @@ public function Ago($includeSeconds = true, $significance = 2)
$now = DBDatetime::now()->getTimestamp();
if ($timestamp <= $now) {
return _t(
'SilverStripe\\ORM\\FieldType\\DBDate.TIMEDIFFAGO',
__CLASS__ . '.TIMEDIFFAGO',
"{difference} ago",
'Natural language time difference, e.g. 2 hours ago',
['difference' => $this->TimeDiff($includeSeconds, $significance)]
);
} else {
return _t(
'SilverStripe\\ORM\\FieldType\\DBDate.TIMEDIFFIN',
"in {difference}",
'Natural language time difference, e.g. in 2 hours',
['difference' => $this->TimeDiff($includeSeconds, $significance)]
);
}
return _t(
__CLASS__ . '.TIMEDIFFIN',
"in {difference}",
'Natural language time difference, e.g. in 2 hours',
['difference' => $this->TimeDiff($includeSeconds, $significance)]
);
}

/**
Expand All @@ -423,20 +422,24 @@ public function TimeDiff($includeSeconds = true, $significance = 2)
$time = $this->getTimestamp();
$ago = abs($time - $now);
if ($ago < 60 && !$includeSeconds) {
return _t('SilverStripe\\ORM\\FieldType\\DBDate.LessThanMinuteAgo', 'less than a minute');
} elseif ($ago < $significance * 60 && $includeSeconds) {
return _t(__CLASS__ . '.LessThanMinuteAgo', 'less than a minute');
}
if ($ago < $significance * 60 && $includeSeconds) {
return $this->TimeDiffIn('seconds');
} elseif ($ago < $significance * 3600) {
}
if ($ago < $significance * 3600) {
return $this->TimeDiffIn('minutes');
} elseif ($ago < $significance * 86400) {
}
if ($ago < $significance * 86400) {
return $this->TimeDiffIn('hours');
} elseif ($ago < $significance * 86400 * 30) {
}
if ($ago < $significance * 86400 * 30) {
return $this->TimeDiffIn('days');
} elseif ($ago < $significance * 86400 * 365) {
}
if ($ago < $significance * 86400 * 365) {
return $this->TimeDiffIn('months');
} else {
return $this->TimeDiffIn('years');
}
return $this->TimeDiffIn('years');
}

/**
Expand All @@ -456,47 +459,47 @@ public function TimeDiffIn($format)
$time = $this->getTimestamp();
$ago = abs($time - $now);
switch ($format) {
case "seconds":
case 'seconds':
$span = $ago;
return _t(
__CLASS__ . '.SECONDS_SHORT_PLURALS',
'{count} sec|{count} secs',
['count' => $span]
);

case "minutes":
case 'minutes':
$span = round($ago / 60);
return _t(
__CLASS__ . '.MINUTES_SHORT_PLURALS',
'{count} min|{count} mins',
['count' => $span]
);

case "hours":
case 'hours':
$span = round($ago / 3600);
return _t(
__CLASS__ . '.HOURS_SHORT_PLURALS',
'{count} hour|{count} hours',
['count' => $span]
);

case "days":
case 'days':
$span = round($ago / 86400);
return _t(
__CLASS__ . '.DAYS_SHORT_PLURALS',
'{count} day|{count} days',
['count' => $span]
);

case "months":
case 'months':
$span = round($ago / 86400 / 30);
return _t(
__CLASS__ . '.MONTHS_SHORT_PLURALS',
'{count} month|{count} months',
['count' => $span]
);

case "years":
case 'years':
$span = round($ago / 86400 / 365);
return _t(
__CLASS__ . '.YEARS_SHORT_PLURALS',
Expand Down Expand Up @@ -554,6 +557,7 @@ public function IsToday()
* </code>
*
* @param string $adjustment PHP strtotime style
* @return $this
*/
public function modify(string $adjustment): self
{
Expand Down Expand Up @@ -588,7 +592,7 @@ public function scaffoldFormField($title = null, $params = null)
protected function fixInputDate($value)
{
// split
list($year, $month, $day, $time) = $this->explodeDateString($value);
[$year, $month, $day, $time] = $this->explodeDateString($value);

if ((int)$year === 0 && (int)$month === 0 && (int)$day === 0) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/ORM/FieldType/DBDatetime.php
Expand Up @@ -93,7 +93,7 @@ public function Time24()
* Return a date and time formatted as per a CMS user's settings.
*
* @param Member $member
* @return boolean | string A time and date pair formatted as per user-defined settings.
* @return boolean|string A time and date pair formatted as per user-defined settings.
*/
public function FormatFromSettings($member = null)
{
Expand Down Expand Up @@ -150,7 +150,7 @@ public function scaffoldFormField($title = null, $params = null)
'SilverStripe\\Forms\\FormField.EXAMPLE',
'e.g. {format}',
'Example format',
[ 'format' => $date ]
['format' => $date]
))
->setAttribute('placeholder', $dateTimeFormat);

Expand Down
2 changes: 1 addition & 1 deletion src/ORM/FieldType/DBDecimal.php
Expand Up @@ -2,8 +2,8 @@

namespace SilverStripe\ORM\FieldType;

use SilverStripe\ORM\DB;
use SilverStripe\Forms\NumericField;
use SilverStripe\ORM\DB;

/**
* Represents a Decimal field.
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/FieldType/DBDouble.php
Expand Up @@ -2,8 +2,8 @@

namespace SilverStripe\ORM\FieldType;

use SilverStripe\ORM\DB;
use SilverStripe\ORM\Connect\MySQLDatabase;
use SilverStripe\ORM\DB;

/**
* Supports double precision DB types
Expand Down
7 changes: 3 additions & 4 deletions src/ORM/FieldType/DBEnum.php
Expand Up @@ -130,7 +130,7 @@ public function requireField()
* @param string $emptyString
* @return DropdownField
*/
public function formField($title = null, $name = null, $hasEmpty = false, $value = "", $emptyString = null)
public function formField($title = null, $name = null, $hasEmpty = false, $value = '', $emptyString = null)
{

if (!$title) {
Expand All @@ -154,13 +154,12 @@ public function scaffoldFormField($title = null, $params = null)
}

/**
* @param string
*
* @param string $title
* @return DropdownField
*/
public function scaffoldSearchField($title = null)
{
$anyText = _t('SilverStripe\\ORM\\FieldType\\DBEnum.ANY', 'Any');
$anyText = _t(__CLASS__ . '.ANY', 'Any');
return $this->formField($title, null, true, '', "($anyText)");
}

Expand Down

0 comments on commit 4ab36ac

Please sign in to comment.