Skip to content

Fix various bool-type coercions #61103

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

Merged
merged 1 commit into from
Jul 15, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private function generateSignature(\ReflectionClass $class): iterable
yield print_r($attributes, true);
$attributes = [];

yield $class->getDocComment();
yield $class->getDocComment() ?: '';
yield (int) $class->isFinal();
yield (int) $class->isAbstract();

Expand All @@ -145,7 +145,7 @@ private function generateSignature(\ReflectionClass $class): iterable
yield print_r($attributes, true);
$attributes = [];

yield $p->getDocComment();
yield $p->getDocComment() ?: '';
yield $p->isDefault() ? '<default>' : '';
yield $p->isPublic() ? 'public' : 'protected';
yield $p->isStatic() ? 'static' : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ private function assertDirectorySame($expected, $current)
}
$currentFiles[substr($file->getPathname(), \strlen($current))] = $file->getPathname();
}
ksort($expectedFiles);
ksort($currentFiles);

$this->assertSame(array_keys($expectedFiles), array_keys($currentFiles));
foreach ($expectedFiles as $fileName => $filePath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ private function createService(Definition $definition, array &$inlineServices, b
if (!$definition->isDeprecated() && \is_array($factory) && \is_string($factory[0])) {
$r = new \ReflectionClass($factory[0]);

if (0 < strpos($r->getDocComment(), "\n * @deprecated ")) {
if (0 < strpos($r->getDocComment() ?: '', "\n * @deprecated ")) {
trigger_deprecation('', '', 'The "%s" service relies on the deprecated "%s" factory class. It should either be deprecated or its factory upgraded.', $id, $r->name);
}
}
Expand All @@ -1142,7 +1142,7 @@ private function createService(Definition $definition, array &$inlineServices, b
$service = $r->getConstructor() ? $r->newInstanceArgs($arguments) : $r->newInstance();
}

if (!$definition->isDeprecated() && 0 < strpos($r->getDocComment(), "\n * @deprecated ")) {
if (!$definition->isDeprecated() && 0 < strpos($r->getDocComment() ?: '', "\n * @deprecated ")) {
trigger_deprecation('', '', 'The "%s" service relies on the deprecated "%s" class. It should either be deprecated or its implementation upgraded.', $id, $r->name);
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ private function collectLineage(string $class, array &$lineage): void
return;
}
$file = $r->getFileName();
if (str_ends_with($file, ') : eval()\'d code')) {
if ($file && str_ends_with($file, ') : eval()\'d code')) {
$file = substr($file, 0, strrpos($file, '(', -17));
}
if (!$file || $this->doExport($file) === $exportedFile = $this->export($file)) {
Expand Down Expand Up @@ -589,12 +589,13 @@ private function generateProxyClasses(): array
continue;
}
do {
$file = $r->getFileName();
if (str_ends_with($file, ') : eval()\'d code')) {
$file = substr($file, 0, strrpos($file, '(', -17));
}
if (is_file($file)) {
$this->container->addResource(new FileResource($file));
if ($file = $r->getFileName()) {
if (str_ends_with($file, ') : eval()\'d code')) {
$file = substr($file, 0, strrpos($file, '(', -17));
}
if (is_file($file)) {
$this->container->addResource(new FileResource($file));
}
}
$r = $r->getParentClass() ?: null;
} while ($r?->isUserDefined());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,11 @@ private function parseDefinition(\DOMElement $service, string $file, Definition
}

foreach ($this->getChildren($service, 'call') as $call) {
$definition->addMethodCall($call->getAttribute('method'), $this->getArgumentsAsPhp($call, 'argument', $file), XmlUtils::phpize($call->getAttribute('returns-clone')));
$definition->addMethodCall(
$call->getAttribute('method'),
$this->getArgumentsAsPhp($call, 'argument', $file),
XmlUtils::phpize($call->getAttribute('returns-clone')) ?: false
);
}

$tags = $this->getChildren($service, 'tag');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static function getTestFilterData()

$inner[] = new MockSplFileInfo([
'name' => 'unreadable-file.txt',
'contents' => false,
'contents' => '',
'type' => 'file',
'mode' => 'r+', ]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CountryTypeTest extends BaseTypeTestCase

protected function setUp(): void
{
IntlTestHelper::requireIntl($this, false);
IntlTestHelper::requireIntl($this);

parent::setUp();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CurrencyTypeTest extends BaseTypeTestCase

protected function setUp(): void
{
IntlTestHelper::requireIntl($this, false);
IntlTestHelper::requireIntl($this);

parent::setUp();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class IntegerTypeTest extends BaseTypeTestCase

protected function setUp(): void
{
IntlTestHelper::requireIntl($this, false);
IntlTestHelper::requireIntl($this);
$this->previousLocale = \Locale::getDefault();
parent::setUp();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LanguageTypeTest extends BaseTypeTestCase

protected function setUp(): void
{
IntlTestHelper::requireIntl($this, false);
IntlTestHelper::requireIntl($this);

parent::setUp();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LocaleTypeTest extends BaseTypeTestCase

protected function setUp(): void
{
IntlTestHelper::requireIntl($this, false);
IntlTestHelper::requireIntl($this);

parent::setUp();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function testIntlTimeZoneInputWithBcAndIntl()

public function testTimezonesAreSelectableWithIntl()
{
IntlTestHelper::requireIntl($this, false);
IntlTestHelper::requireIntl($this);

$choices = $this->factory->create(static::TESTED_TYPE, null, ['intl' => true])
->createView()->vars['choices'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ public static function provideSessionOptions(): \Generator

yield 'set_cookiesecure_auto_by_symfony_false_by_php' => [
'phpSessionOptions' => ['secure' => false],
'sessionOptions' => ['cookie_path' => '/test/', 'cookie_httponly' => 'auto', 'cookie_secure' => 'auto', 'cookie_samesite' => Cookie::SAMESITE_LAX],
'sessionOptions' => ['cookie_path' => '/test/', 'cookie_httponly' => true, 'cookie_secure' => 'auto', 'cookie_samesite' => Cookie::SAMESITE_LAX],
'expectedSessionOptions' => ['cookie_path' => '/test/', 'cookie_domain' => '', 'cookie_secure' => false, 'cookie_httponly' => true, 'cookie_samesite' => Cookie::SAMESITE_LAX],
];

yield 'set_cookiesecure_auto_by_symfony_true_by_php' => [
'phpSessionOptions' => ['secure' => true],
'sessionOptions' => ['cookie_path' => '/test/', 'cookie_httponly' => 'auto', 'cookie_secure' => 'auto', 'cookie_samesite' => Cookie::SAMESITE_LAX],
'sessionOptions' => ['cookie_path' => '/test/', 'cookie_httponly' => true, 'cookie_secure' => 'auto', 'cookie_samesite' => Cookie::SAMESITE_LAX],
'expectedSessionOptions' => ['cookie_path' => '/test/', 'cookie_domain' => '', 'cookie_secure' => true, 'cookie_httponly' => true, 'cookie_samesite' => Cookie::SAMESITE_LAX],
];

Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Mime/Tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ private function generateSomeParts(): array
public function testAttachments()
{
// inline part
$contents = file_get_contents($name = __DIR__.'/Fixtures/mimetypes/test', 'r');
$contents = file_get_contents($name = __DIR__.'/Fixtures/mimetypes/test');
$att = new DataPart($file = fopen($name, 'r'), 'test');
$inline = (new DataPart($contents, 'test'))->asInline();
$e = new Email();
Expand Down Expand Up @@ -618,7 +618,7 @@ public function testHtmlBodyAcceptedTypes()
$email->html(null);
$this->assertNull($email->getHtmlBody());

$contents = file_get_contents(__DIR__.'/Fixtures/mimetypes/test', 'r');
$contents = file_get_contents(__DIR__.'/Fixtures/mimetypes/test');
$email->html($contents);
$this->assertSame($contents, $email->getHtmlBody());
}
Expand All @@ -641,7 +641,7 @@ public function testTextBodyAcceptedTypes()
$email->text(null);
$this->assertNull($email->getTextBody());

$contents = file_get_contents(__DIR__.'/Fixtures/mimetypes/test', 'r');
$contents = file_get_contents(__DIR__.'/Fixtures/mimetypes/test');
$email->text($contents);
$this->assertSame($contents, $email->getTextBody());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function disableMagicSet(): static
*/
public function isMagicCallEnabled(): bool
{
return (bool) ($this->magicMethods & PropertyAccessor::MAGIC_CALL);
return $this->magicMethods & PropertyAccessor::MAGIC_CALL;
Copy link
Contributor

Choose a reason for hiding this comment

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

isnt this producing an integer now?

Copy link
Member

Choose a reason for hiding this comment

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

see the return type, which cannot be bypassed

Copy link
Member

Choose a reason for hiding this comment

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

The return type will create an implicit cast to bool, which is precisely what @Girgias tries to deprecated in PHP 8.5 AFAICT.

Copy link
Member

Choose a reason for hiding this comment

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

correct, but this is not done yet :)

Copy link
Contributor

Choose a reason for hiding this comment

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

i dont understand it, why would you want to rely on not using strict_types=1 currently? also, why opening up a deprecation, if the future fix is adding back (bool) again?

Copy link
Member

Choose a reason for hiding this comment

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

Strict types was a bad idea that makes writing PHP more fragile, see https://github.com/Girgias/unify-typing-modes-rfc?tab=readme-ov-file#unintended-consequences-of-strict_types-mode
You might not agree but that's nonetheless the way Symfony does types.
The future is not adding back the cast. At least not before any vote happened on the topic.
So this change is just bringing consistency with the current policies.

Copy link
Contributor

Choose a reason for hiding this comment

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

right, i understand the POV a bit better now.

that said

Use of explicit casts to conform to type requirements

im fine with explicitness :) i mean, psalm complains about it 😅

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static function getInvalidIsbn()

public function testNullIsValid()
{
$constraint = new Isbn(true);
$constraint = new Isbn();

$this->validator->validate(null, $constraint);

Expand All @@ -130,7 +130,7 @@ public function testNullIsValid()

public function testEmptyStringIsValid()
{
$constraint = new Isbn(true);
$constraint = new Isbn();

$this->validator->validate('', $constraint);

Expand All @@ -140,7 +140,7 @@ public function testEmptyStringIsValid()
public function testExpectsStringCompatibleType()
{
$this->expectException(UnexpectedValueException::class);
$constraint = new Isbn(true);
$constraint = new Isbn();

$this->validator->validate(new \stdClass(), $constraint);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Workflow/Dumper/GraphvizDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ protected function findTransitions(Definition $definition, bool $withMetadata):
/**
* @internal
*/
protected function addPlaces(array $places, float $withMetadata): string
protected function addPlaces(array $places, bool $withMetadata): string
{
$code = '';

Expand Down Expand Up @@ -303,7 +303,7 @@ protected function addAttributes(array $attributes): string
*
* @internal
*/
protected function formatLabel(Definition $definition, string $withMetadata, array $options): string
protected function formatLabel(Definition $definition, bool $withMetadata, array $options): string
{
$currentLabel = $options['label'] ?? '';

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy