Code Monkey home page Code Monkey logo

Comments (6)

ondrejmirtes avatar ondrejmirtes commented on June 3, 2024 2

It's hard to help you without any code sample, but I'll guess:

If your entity repositories have a common parent class, you can set it: https://github.com/phpstan/phpstan-doctrine#configuration

If these methods come from specific repository for an entity, you should mark it in a more specific way:

/** @var MyApp\CustomerRepository<x\Entity\Customer> */
private $repository;

Also make sure that you set your objectManagerLoader (see README) and that you have set repositoryClass above your entity.

from phpstan-doctrine.

ondrejmirtes avatar ondrejmirtes commented on June 3, 2024 1

Hi, PHPStan does not see the connection between UserRepository class and User entity. Repository classes can be reused for multiple entity types so this typehint isn't sufficient for your purpose. That's why currently you need to use App\Repository\UserRepository<App\Entity\User> annotation.

But what could PHPStan do is to list all the entities where specific repository class is used, and use this list as potential return types from find* etc. methods (where the generic type is missing). Please open a new issue (feature request) here.

from phpstan-doctrine.

Kocal avatar Kocal commented on June 3, 2024

Sorry to post here, but that means that we can't use repositories as a service in Symfony context?

Given the following repository:

<?php

namespace App\Repository\User;

use App\Entity\User\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
 * @method User|null find($id, $lockMode = null, $lockVersion = null)
 * @method User|null findOneBy(array $criteria, array $orderBy = null)
 * @method User[]    findAll()
 * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class UserRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, User::class);
    }

    // ...
}

The following code is not correctly analyzed by PhpStan:

<?php

namespace App\Controller;

use App\Repository\User\UserRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class HomeController extends AbstractController
{
    /**
     * @Route("/", name="home")
     */
    public function index(UserRepository $userRepository)
    {
        $user = $userRepository->find(123);
        $user->getFooBar(); // should fail, but it does not
    }
}

Here I expect the following error:

 ------ -------------------------------------------------------------- 
  Line   Controller/HomeController.php                                 
 ------ -------------------------------------------------------------- 
  17     Cannot call method getFooBar() on App\Entity\User\User|null.  
 ------ -------------------------------------------------------------- 

                                                                                                                        
 [ERROR] Found 1 error                                                                                                  
                                                                                                                        

But nothing appears as long I'm using vendor/phpstan/phpstan-doctrine/extension.neon in my phpstan.neon config file.

I tried to use the annotation /** @var App\Repository\UserRepository<App\Entity\User> */ and it works well with doctrine extension, but this is really cumbersome and verbose.
To be honest I don't think someone will actually use this, since we can use type hinting UserRepository from a method. 😕
And even more if multiples repositories should be used in different places. 😕

tl;dr: add @method annotations for find* methods, don't use doctrine extension, and it works

Is it possible to support repository as service?
Thanks!

from phpstan-doctrine.

curtchan avatar curtchan commented on June 3, 2024

Not sure how #67 is something that needs work on? Been doing repository as service approach since Symfony 2.1 and it's approach which phpstan handles well (it's just a service + you define return types manually, so it doesnt have to guess return types). The only problem i see is with EntityRepositories that are defined on annotation-level. Sure, if you have common methods, you can extract them into one abstract class and configure phpstan to use it, but when the methods differ - as i understand you can only define one such class so there starts the problem

from phpstan-doctrine.

ondrejmirtes avatar ondrejmirtes commented on June 3, 2024

The problem is with UserRepository vs. UserRepository<User>. With UserRepository, PHPStan needs to figure out that User is the only entity which UserRepository is used at (which doesn't always have to be true) in order to know return types of methods from EntityRepository like find or findOneBy...

from phpstan-doctrine.

github-actions avatar github-actions commented on June 3, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

from phpstan-doctrine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.