Skip to content

param type can be a string but not class-string #10976

Closed Answered by ondrejmirtes
metaturso asked this question in Support
Discussion options

You must be logged in to vote

So the idea is that we're going to make a new custom PHPDoc type thanks to https://phpstan.org/developing-extensions/custom-phpdoc-types.

Step 1: Implement NonClassStringType

namespace App;

use PHPStan\Type\AcceptsResult;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;

class NonClassStringType extends StringType
{

	public function describe(VerbosityLevel $level): string
	{
		return 'non-class-string';
	}

	public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
	{
		if ($type->isClassStringType()->yes()) {
			return AcceptsResult::createNo();
		}

		return parent::acceptsWithReason($type, $strictTypes);
	}

}

That's basically…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@metaturso
Comment options

@ondrejmirtes
Comment options

Comment options

You must be logged in to vote
1 reply
@metaturso
Comment options

Answer selected by metaturso
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Support
Labels
None yet
2 participants