Comments (6)
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.
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.
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.
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.
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.
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)
- MappingException since 1.3.55 HOT 2
- Performance issues after 1.3.54 HOT 19
- PHPStan crashes if Doctrine Column is a UnitEnum (PHPStan+Doctrine extension) HOT 2
- False Possitive in Abstract Respoitory classes mapped to Abstract Entity classes: "<entity> has no field or association named <field>" HOT 5
- Comparing the database mapping type to the property type should take the phpstan level into account HOT 2
- Compatibility with doctrine/orm:3 HOT 9
- doctrine/orm v3 - Declaration of ClassMetadataFactory::newClassMetadataInstance($className) HOT 4
- Incorrect PHPStan Error for Method Return Type Declaration HOT 2
- Enforce `Connection::executeQuery` on SELECT and `Connection::executeStatement` on others queries HOT 7
- Repository methods considered pure
- support for doctrine/orm 3+? HOT 6
- Query analyser gets confused when a join alias is equal to the relation name
- Use DTO in a query builder HOT 7
- Add UniqueEntity constraint field validation
- False positive when working with ArrayAccess HOT 21
- Since v1.3.64: Interface "PHPStan\Classes\ForbiddenClassNameExtension" not found HOT 6
- Incompatible bleedingEdge Connection stub typehint with DBAL 4 HOT 5
- Report an error for QueryBuilder::setParameter without specifying the Type. HOT 10
- Query result type is not recognized if the alias is `"attribute"`
- Autogenerated id column should be positive-int HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpstan-doctrine.