Skip to content

Commit

Permalink
Merge pull request #1947 from propelorm/fixes-date
Browse files Browse the repository at this point in the history
Fix up datetime PHPStan error.
  • Loading branch information
dereuromark committed Jan 23, 2023
2 parents de6deb4 + 531dd64 commit 5aec365
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Propel/Common/Config/XmlToArrayConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function convert(string $xmlToParse): array
libxml_clear_errors();
libxml_use_internal_errors($currentInternalErrors);

if (count($errors) > 0) {
if ($xml === false || count($errors) > 0) {
throw new XmlParseException($errors);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function objectCall(ObjectBuilder $builder): string
public function objectFilter(string &$script): void
{
$p = new PhpParser($script, true);
$text = $p->findMethod('toArray');
$text = (string)$p->findMethod('toArray');
$matches = [];
preg_match('/(\$result = \[([^;]+)\];)/U', $text, $matches);
if (!$matches) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ public function objectFilter(string &$script, AbstractOMBuilder $builder): void
$script = str_replace($search, $replace, $script);
}
} else {
/** @var string $scope */
$scope = current($this->behavior->getScopes());
$name = strtolower($this->behavior->getTable()->getColumn($scope)->getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,10 @@ static public function sortableApplyScopeCriteria(Criteria \$criteria, \$scope,
";
}
} else {
/** @var string $scope */
$scope = current($this->behavior->getScopes());
$script .= "
\$criteria->\$method({$this->tableMapClassName}::" . Column::CONSTANT_PREFIX . strtoupper(current($this->behavior->getScopes())) . ", \$scope, Criteria::EQUAL);
\$criteria->\$method({$this->tableMapClassName}::" . Column::CONSTANT_PREFIX . strtoupper($scope) . ", \$scope, Criteria::EQUAL);
";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ protected function sortRecursive(array &$array): void
/**
* @param \Propel\Generator\Model\Table $table
*
* @return string
* @return class-string<\Propel\Runtime\Map\TableMap>
*/
protected function getFullyQualifiedTableMapClassName(Table $table): string
{
$builder = new TableMapBuilder($table);
$builder->setGeneratorConfig($this->generatorConfig);

/** @var class-string<\Propel\Runtime\Map\TableMap> */
return $builder->getFullyQualifiedClassName();
}

Expand Down
1 change: 1 addition & 0 deletions src/Propel/Runtime/DataFetcher/AbstractDataFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function getDataObject()
*/
public function fetchColumn($index = null)
{
/** @var array|null $next */
$next = $this->fetch();

if (!$next) {
Expand Down
61 changes: 42 additions & 19 deletions src/Propel/Runtime/Util/PropelDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,51 @@ public static function newInstance($value, ?DateTimeZone $timeZone = null, strin
// because DateTime('') == DateTime('now') -- which is unexpected
return null;
}

try {
if (static::isTimestamp($value)) { // if it's a unix timestamp
$format = 'U';
if (strpos($value, '.')) {
//with milliseconds
$format = 'U.u';
}

$dateTimeObject = DateTime::createFromFormat($format, $value, new DateTimeZone('UTC'));
// timezone must be explicitly specified and then changed
// because of a DateTime bug: http://bugs.php.net/bug.php?id=43003
$dateTimeObject->setTimeZone(new DateTimeZone(date_default_timezone_get()));
$dateTimeObject = static::createDateTime($value, $timeZone, $dateTimeClass);
} catch (Exception $e) {
$value = var_export($value, true);

throw new PropelException('Error parsing date/time value `' . $value . '`: ' . $e->getMessage(), 0, $e);
}

return $dateTimeObject;
}

/**
* @param mixed $value
* @param \DateTimeZone|null $timeZone
* @param string $dateTimeClass
*
* @throws \Exception
*
* @return mixed
*/
protected static function createDateTime($value, ?DateTimeZone $timeZone = null, string $dateTimeClass = 'DateTime')
{
if (static::isTimestamp($value)) { // if it's a unix timestamp
$format = 'U';
if (strpos($value, '.')) {
//with milliseconds
$format = 'U.u';
}

$dateTimeObject = DateTime::createFromFormat($format, $value, new DateTimeZone('UTC'));
if ($dateTimeObject === false) {
throw new Exception(sprintf('Cannot create DateTime from format `%s`', $format));
}

// timezone must be explicitly specified and then changed
// because of a DateTime bug: http://bugs.php.net/bug.php?id=43003
$dateTimeObject->setTimeZone(new DateTimeZone(date_default_timezone_get()));
} else {
if ($timeZone === null) {
// stupid DateTime constructor signature
$dateTimeObject = new $dateTimeClass($value);
} else {
if ($timeZone === null) {
// stupid DateTime constructor signature
$dateTimeObject = new $dateTimeClass($value);
} else {
$dateTimeObject = new $dateTimeClass($value, $timeZone);
}
$dateTimeObject = new $dateTimeClass($value, $timeZone);
}
} catch (Exception $e) {
throw new PropelException('Error parsing date/time value: ' . var_export($value, true), 0, $e);
}

return $dateTimeObject;
Expand Down

0 comments on commit 5aec365

Please sign in to comment.