Skip to content

Support for static method calls for queries #765

@xPaw

Description

@xPaw

I have a little wrapper class for PDO queries that basically boils down to this:

<?php
class DB
{
	public static function Instance() : \Pdo\Mysql
	{
		return PDO::connect();
	}

	/**
	 * @param literal-string $query
	 *
	 * @see \PDO::prepare()
	 */
	public static function Prepare( string $query ) : \PDOStatement
	{
		return self::Instance()->prepare( $query );
	}

	/**
	 * @param literal-string $query
	 *
	 * @see \PDO::query()
	 */
	public static function Query( string $query ) : \PDOStatement
	{
		return self::Instance()->query( $query );
	}
}

I tried the following config:

services:
	-
		class: staabm\PHPStanDba\Rules\SyntaxErrorInQueryMethodRule
		tags: [phpstan.rules.rule]
		arguments:
			classMethods:
				- 'DB::Query#0'
				- 'DB::Prepare#0'
	-
		class: staabm\PHPStanDba\Rules\QueryPlanAnalyzerRule
		tags: [phpstan.rules.rule]
		arguments:
			classMethods:
				- 'DB::Query#0'
				- 'DB::Prepare#0'

But after looking at these rules, they only implement MethodCall, while I believe static methods would be StaticCall, unfortunately SyntaxErrorInQueryFunctionRule also does not work for this.

Is the solution here to add a 3rd rule to handle static calls, and then another if check in QueryPlanAnalyzerRule?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions