| Total Complexity | 12 | 
| Total Lines | 75 | 
| Duplicated Lines | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
| 1 | <?php  | 
            ||
| 15 | class ProxyGenerator  | 
            ||
| 16 | { | 
            ||
| 17 | /** @var Configuration */  | 
            ||
| 18 | private $configuration;  | 
            ||
| 19 | /** @var AccessInterceptorValueHolderFactory */  | 
            ||
| 20 | private $factory;  | 
            ||
| 21 | |||
| 22 | public function __construct(Configuration $configuration, ?AccessInterceptorValueHolderFactory $factory = null)  | 
            ||
| 23 |     { | 
            ||
| 24 | $this->configuration = $configuration;  | 
            ||
| 25 | $this->factory = $factory ?? new AccessInterceptorValueHolderFactory();  | 
            ||
| 26 | }  | 
            ||
| 27 | |||
| 28 | public function generate(object $object, array $methods = []): object  | 
            ||
| 29 |     { | 
            ||
| 30 | $objectManager = $this->configuration->objectManagerFor(\get_class($object));  | 
            ||
| 31 |         $interceptor = function(object $proxy, object $model) use ($objectManager): void { | 
            ||
| 32 | $autoRefreshEnabled = \property_exists($model, '_foundry_autoRefresh') ? $model->_foundry_autoRefresh : true;  | 
            ||
| 33 |             if (!$autoRefreshEnabled) { | 
            ||
| 34 | return;  | 
            ||
| 35 | }  | 
            ||
| 36 | |||
| 37 | $modelClassMetadata = $objectManager->getClassMetadata(\get_class($model));  | 
            ||
| 38 | |||
| 39 | // only check for changes if the object is managed in the current om  | 
            ||
| 40 |             if ($objectManager instanceof EntityManagerInterface && $objectManager->contains($model)) { | 
            ||
| 41 | // cannot use UOW::recomputeSingleEntityChangeSet() here as it wrongly computes embedded objects as changed  | 
            ||
| 42 | $objectManager->getUnitOfWork()->computeChangeSet($modelClassMetadata, $model);  | 
            ||
| 43 | |||
| 44 |                 if (!empty($objectManager->getUnitOfWork()->getEntityChangeSet($model))) { | 
            ||
| 45 |                     throw new \RuntimeException(\sprintf('Cannot auto refresh "%s" as there are unsaved changes. Be sure to call ->save() or disable auto refreshing (see https://github.com/zenstruck/foundry#auto-refresh for details).', \get_class($model))); | 
            ||
| 46 | }  | 
            ||
| 47 | |||
| 48 | $objectManager->refresh($model);  | 
            ||
| 49 | |||
| 50 | return;  | 
            ||
| 51 | }  | 
            ||
| 52 | |||
| 53 | // refetch the model as it's no longer managed  | 
            ||
| 54 | $modelId = $modelClassMetadata->getIdentifierValues($model);  | 
            ||
| 55 |             if (!$modelId) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 56 | // TODO: throw exception?  | 
            ||
| 57 | return;  | 
            ||
| 58 | }  | 
            ||
| 59 | |||
| 60 | $proxy->setWrappedValueHolder($objectManager->find(\get_class($model), $modelId));  | 
            ||
| 61 | };  | 
            ||
| 62 | |||
| 63 | $methodsToIntercept = $this->filterGlob(\get_class_methods($object), $methods);  | 
            ||
| 64 | |||
| 65 | return $this->factory->createProxy($object, \array_fill_keys($methodsToIntercept, $interceptor));  | 
            ||
| 66 | }  | 
            ||
| 67 | |||
| 68 | private function filterGlob(array $objectMethods, array $methodFilter): array  | 
            ||
| 90 | }  | 
            ||
| 91 | }  | 
            ||
| 92 | 
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.