Skip to content

Conversation

@jaymcp
Copy link
Contributor

@jaymcp jaymcp commented Mar 12, 2025

Description

Introduces a new Sniff that will flag when a class that defines the __toString magic method doesn't implement the Stringable interface.

This Sniff includes a fixer.

Testing Steps

Install this PR branch:

composer require --dev bigbite/phpcs-config:dev-feat/stringable

Create a file called class-my-class.php:

<?php

class My_Class {
	public function __toString(): string {
		return __CLASS__;
	}
}

Run PHPCS:

./vendor/bin/phpcs -ps --standard=BigBite class-my-class.php

If on PHP > 8.0:
Amongst the errors should be one labelled BigBite.Classes.Stringable, which should inform you that the class should implement the Stringable interface.

If on PHP < 8.0:
There should not be an error labelled BigBite.Classes.Stringable amongst the list of errors, since the Stringable interface was only introduced in PHP 8.0.

Types of changes (if applicable):

  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).

Checklist (if applicable):

  • All checks pass when running composer run all-checks.

@jaymcp jaymcp force-pushed the feat/stringable branch 2 times, most recently from 3381db3 to 120328d Compare March 12, 2025 18:56
@jaymcp jaymcp requested a review from a team March 14, 2025 09:27
@jaymcp jaymcp marked this pull request as ready for review March 17, 2025 12:45
@jaymcp jaymcp merged commit 6bade42 into main Apr 4, 2025
5 checks passed
@jaymcp jaymcp deleted the feat/stringable branch April 4, 2025 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants