Solucionado (ver solução)

Importante

Você está vendo a versão anterior da nova experiência da Alura que estamos preparando para você. Em breve, ela ganha uma identidade visual novinha totalmente pensada em potencializar seus estudos!

Solucionado
(ver solução)
11
respostas

Composer phan Classe Student

Boa noite Professor.

Eu usei o phan para verificar o código no projeto de PDO (praticar um pouco composer), e apareceu um comentário sobre o type do id , que null não é compátivel, poderia esclarecer a resposta do phan,pois não entendi muito bem.

src\Domain\Model\Student.php:13 PhanPossiblyNullTypeMismatchProperty Assigning $this->id of type ?int to property but \Alura\Pdo\Domain\Model\Student->id is int (null is incompatible)

11 respostas

Me mostra seu código todo, Bruno, por favor?

Código da classe Student

<?php

namespace Alura\Pdo\Domain\Model;

class Student
{
    private ?int $id;
    private string $name;
    private \DateTimeInterface $birthDate;

    public function __construct(?int $id, string $name, \DateTimeInterface $birthDate)
    {
        $this->id = $id;
        $this->name = $name;
        $this->birthDate = $birthDate;
    }

    public function id(): ?int
    {
        return $this->id;
    }

    public function name(): string
    {
        return $this->name;
    }

    public function birthDate(): \DateTimeInterface
    {
        return $this->birthDate;
    }

    public function age(): int
    {
        return $this->birthDate
            ->diff(new \DateTimeImmutable())
            ->y;
    }
}

Fala, Bruno.

Parece tudo ok. Tenta atualizaro Phan e vê se continua apontando o erro.

Fala, Bruno.

Atualizar resolveu

Boa noite professor.

Então , eu rodei o composer update, e insiste nesse mesmo erro.

Me passa seu arquivo .phan/config.php pra eu testar aqui, Bruno?

<?php

/**

  • This configuration will be read and overlaid on top of the

  • default configuration. Command line arguments will be applied

  • after this file is read. */ return [

    // Supported values: '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', null. // If this is set to null, // then Phan assumes the PHP version which is closest to the minor version // of the php executable used to execute Phan. "target_php_version" => null,

    // A list of directories that should be parsed for class and // method information. After excluding the directories // defined in exclude_analysis_directory_list, the remaining // files will be statically analyzed for errors. // // Thus, both first-party and third-party code being used by // your application should be included in this list. 'directory_list' => [

     'src',
     'vendor/symfony/console',
     '/'

    ],

    // A directory list that defines files that will be excluded // from static analysis, but whose class and method // information should be included. // // Generally, you'll want to include the directories for // third-party code (such as "vendor/") in this list. // // n.b.: If you'd like to parse but not analyze 3rd // party code, directories containing that code // should be added to the directory_list as // to exclude_analysis_directory_list. "exclude_analysis_directory_list" => [

     'vendor/'

    ],

    // A list of plugin files to execute. // Plugins which are bundled with Phan can be added here by providing their name // (e.g. 'AlwaysReturnPlugin') // // Documentation about available bundled plugins can be found // at https://github.com/phan/phan/tree/master/.phan/plugins // // Alternately, you can pass in the full path to a PHP file // with the plugin's implementation (e.g. 'vendor/phan/phan/.phan/plugins/AlwaysReturnPlugin.php') 'plugins' => [

     // checks if a function, closure or method unconditionally returns.
     // can also be written as 'vendor/phan/phan/.phan/plugins/AlwaysReturnPlugin.php'
     'AlwaysReturnPlugin',
     'DollarDollarPlugin',
     'DuplicateArrayKeyPlugin',
     'DuplicateExpressionPlugin',
     'PregRegexCheckerPlugin',
     'PrintfCheckerPlugin',
     'SleepCheckerPlugin',
     // Checks for syntactically unreachable statements in
     // the global scope or function bodies.
     'UnreachableCodePlugin',
     'UseReturnValuePlugin',
     'EmptyStatementListPlugin',
     'LoopVariableReusePlugin',

    ], ];

Bruno, anotando a propriedade com `/** @var int|null */ fez o phan calar a boca aqui. Parece ser um falso positivo.

Reporta aqui pra eles darem uma olhada: https://github.com/phan/phan/issues

Eu adiciono o /** @var int|null */ no arquivo config dentro do .phan ?

solução!

Não, Bruno. Na propriedade que ele reclama estar errada (o ID, no caso)

Funcionou, valeu!!