Skip to content

[TASK] Deprecate __toString() #1006

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
Feb 27, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Please also have a look at our

### Deprecated

- Deprecate `__toString()` (#1006)
- `OutputFormat` properties for space around list separators as an array (#880)

### Removed
Expand Down
3 changes: 3 additions & 0 deletions src/CSSList/AtRuleBlockList.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function atRuleArgs(): string
return $this->arguments;
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
3 changes: 3 additions & 0 deletions src/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ public function removeDeclarationBlockBySelector($selectors, $removeAll = false)
}
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
3 changes: 3 additions & 0 deletions src/CSSList/KeyFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public function getAnimationName()
return $this->animationName;
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
3 changes: 3 additions & 0 deletions src/Comment/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public function setComment(string $commentText): void
$this->commentText = $commentText;
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
2 changes: 1 addition & 1 deletion src/OutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function safely(callable $callable): ?string
}

/**
* Clone of the `implode` function, but calls `render` with the current output format instead of `__toString()`.
* Clone of the `implode` function, but calls `render` with the current output format.
*
* @param array<array-key, Renderable|string> $values
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Property/CSSNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public function getLineNo(): int
return $this->lineNumber;
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
3 changes: 3 additions & 0 deletions src/Property/Charset.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public function getCharset()
return $this->charset->getString();
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
3 changes: 3 additions & 0 deletions src/Property/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public function getLocation()
return $this->location;
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
3 changes: 3 additions & 0 deletions src/Property/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public function setSelector($selector): void
$this->specificity = null;
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->getSelector();
Expand Down
3 changes: 3 additions & 0 deletions src/Renderable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

interface Renderable
{
/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string;

public function render(OutputFormat $outputFormat): string;
Expand Down
3 changes: 3 additions & 0 deletions src/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ public function getIsImportant()
return $this->isImportant;
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
3 changes: 3 additions & 0 deletions src/RuleSet/AtRuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public function atRuleArgs()
return $this->arguments;
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
2 changes: 2 additions & 0 deletions src/RuleSet/DeclarationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public function getSelectors()

/**
* @throws OutputException
*
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
Expand Down
3 changes: 3 additions & 0 deletions src/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ public function removeRule($mRule): void
}
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
3 changes: 3 additions & 0 deletions src/Value/CSSFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public function getArguments()
return $this->aComponents;
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
3 changes: 3 additions & 0 deletions src/Value/CSSString.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public function getString()
return $this->string;
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
3 changes: 3 additions & 0 deletions src/Value/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ public function getColorDescription()
return $this->getName();
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
3 changes: 3 additions & 0 deletions src/Value/LineName.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public static function parse(ParserState $parserState): LineName
return new LineName($aNames, $parserState->currentLine());
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
3 changes: 3 additions & 0 deletions src/Value/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ public function isRelative(): bool
return false;
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
3 changes: 3 additions & 0 deletions src/Value/URL.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public function getURL()
return $this->oURL;
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
3 changes: 3 additions & 0 deletions src/Value/ValueList.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public function setListSeparator($sSeparator): void
$this->sSeparator = $sSeparator;
}

/**
* @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
*/
public function __toString(): string
{
return $this->render(new OutputFormat());
Expand Down
13 changes: 0 additions & 13 deletions tests/Functional/Comment/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@
*/
final class CommentTest extends TestCase
{
/**
* @test
*/
public function toStringRendersCommentEnclosedInCommentDelimiters(): void
{
$comment = 'There is no spoon.';
$subject = new Comment();

$subject->setComment($comment);

self::assertSame('/*' . $comment . '*/', (string) $subject);
}

/**
* @test
*/
Expand Down
Empty file.
27 changes: 27 additions & 0 deletions tests/FunctionalDeprecated/Comment/CommentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Sabberworm\CSS\Tests\FunctionalDeprecated\Comment;

use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\Comment\Comment;

/**
* @covers \Sabberworm\CSS\Comment\Comment
*/
final class CommentTest extends TestCase
{
/**
* @test
*/
public function toStringRendersCommentEnclosedInCommentDelimiters(): void
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this method being moved here to test something that's deprecated? Shouldn't the introduction of the test class be a separate PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm moving this one test from Tests/Functional/ to Tests/FunctionalDeprecated/, where tests for deprecated functionality should be placed. (This will allow us to have different PHPUnit configuration files later on, allowing calls to deprecated functionality in Tests/FunctionalDeprecated/, but not in Tests/Functional/.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Apologies. I overlooked the location of where this was being moved to, and instead assumed (without looking) it was a new test class being created in the tests/Unit directory.

That said, I'm now wondering why the CommentTest class is in Functional rather than Unit, given that it appears to be a unit test for the Comment class - but that's beyond the scope of this PR.

{
$comment = 'There is no spoon.';
$subject = new Comment();

$subject->setComment($comment);

self::assertSame('/*' . $comment . '*/', (string) $subject);
}
}