Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up datetime PHPStan error. #1947

Merged
merged 1 commit into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
dereuromark marked this conversation as resolved.
Show resolved Hide resolved
$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 */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think fetch() returns false if there are no more columns to fetch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we could check on this. I just thought in this context it seems impossible (theoretical problem).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the check happens in the next line:

        if (!$next) {
            return null;
        }

Copy link
Contributor Author

@dereuromark dereuromark Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented is array|bool|null

The bool true is the problem here
And instead of just checking on this, I simplified to the expected outcome as docblock.

$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