"""self::metodo() / static::metodo() chamados dentro da classe, mas nao declarados static.

PHP 8: fatal "Non-static method X::y() cannot be called statically".
"""
import re
from pathlib import Path

MART = Path(r"C:\projetos\apps\martinelli-php83")
IGNORAR = ("vendor/", "runtime/", "web/assets/", "nbproject/", ".git/", "web/plugins/")

CHAMADA = re.compile(r"\b(?:self|static)::([a-zA-Z_][A-Za-z0-9_]*)\s*\(")


def main():
    total = 0
    for p in MART.rglob("*.php"):
        rel = p.relative_to(MART).as_posix()
        if any(s in rel for s in IGNORAR):
            continue
        t = p.read_bytes().decode("utf-8", errors="replace")
        metodos = set(CHAMADA.findall(t))
        if not metodos:
            continue
        for met in sorted(metodos):
            decl = re.search(r"^([ \t]*)(public|protected|private)\s+(static\s+)?function\s+" + re.escape(met) + r"\s*\(",
                             t, re.M)
            if decl is None:
                continue
            if decl.group(3):
                continue
            print(f"{rel}: self/static::{met} -> nao e static (linha {t[:decl.start()].count(chr(10)) + 1})")
            total += 1
    print(f"total: {total}")


if __name__ == "__main__":
    main()
