Issues (82)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

Form/DataTransformer/RestCollectionTransformer.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Pgs\RestfonyBundle\Form\DataTransformer;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
9
use Symfony\Component\Form\DataTransformerInterface;
10
use Symfony\Component\Form\Exception\TransformationFailedException;
11
12
/**
13
 * Transforms Relationship data for entity based forms.
14
 */
15
class RestCollectionTransformer implements DataTransformerInterface
16
{
17
    /**
18
     * @var ObjectManager
19
     */
20
    private $entityManager;
21
22
    /**
23
     * The name of the entity we are working with.
24
     *
25
     * @var string
26
     */
27
    private $entityName;
28
29
    /**
30
     * @param ObjectManager $entityManager
31
     * @param string $entityName
32
     */
33 16
    public function __construct(ObjectManager $entityManager, $entityName)
34
    {
35 16
        $this->entityManager = $entityManager;
36 16
        $this->entityName = $entityName;
37 16
    }
38
39
    /**
40
     * Transforms an entity collection to an array of identifiers.
41
     *
42
     * @param Collection|null $collection
43
     *
44
     * @return string[]
45
     */
46 8
    public function transform($collection)
47
    {
48 8
        if (empty($collection)) {
49 1
            return [];
50
        }
51
52 7
        if (!$collection instanceof Collection) {
53 1
            throw new TransformationFailedException(sprintf(
54 1
                '%s is not an instance of %s',
55
                gettype($collection),
56 1
                'Doctrine\Common\Collections\Collection'
57
            ));
58
        }
59
60
        return $collection->map(function ($entity) {
61
            try {
62 5
                $entityString = (string) $entity;
63 5
            } catch (\Exception $e) {
0 ignored issues
show
catch (\Exception $e) { ...($metadata, $entity); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
64 5
                $metadata = $this->entityManager->getClassMetadata(get_class($entity));
65 5
                $entityString = $this->getEntityIdentifier($metadata, $entity);
66
            }
67
68 4
            return $entityString;
69 6
        })->toArray();
70
    }
71
72
    /**
73
     * Transforms an array of ids to an array of entities.
74
     *
75
     * @param Collection|array $collection
76
     *
77
     * @return Collection
78
     *
79
     * @throws TransformationFailedException if entity is not found.
80
     */
81 7
    public function reverseTransform($collection)
82
    {
83
        //convert plain arrays to a doctrine collection
84 7
        if (is_array($collection)) {
85 5
            $collection = new ArrayCollection($collection);
86
        }
87
88 7
        if (!$collection instanceof Collection) {
89 1
            throw new TransformationFailedException(sprintf(
90 1
                '%s is not an instance of %s',
91
                gettype($collection),
92 1
                'Doctrine\Common\Collections\Collection'
93
            ));
94
        }
95
96 6
        if ($collection->isEmpty()) {
97 2
            return $collection;
98
        }
99
100 4
        return $collection->map(function ($id) {
101 4
            $entity = $this->entityManager
102 4
                ->getRepository($this->entityName)
103 4
                ->find($id)
104
            ;
105
106 4
            if (null === $entity) {
107 1
                throw new TransformationFailedException(sprintf(
108 1
                    'A %s with id "%s" does not exist!',
109 1
                    $this->entityName,
110
                    $id
111
                ));
112
            }
113
114 3
            return $entity;
115 4
        });
116
    }
117
118
    /**
119
     * @param ClassMetadata $metadata
120
     * @param mixed         $entity   The entity.
121
     *
122
     * @throws \RuntimeException
123
     *
124
     * @return mixed
125
     */
126 5 View Code Duplication
    protected function getEntityIdentifier(ClassMetadata $metadata, $entity)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
    {
128 5
        if (count($metadata->getIdentifierFieldNames()) !== 1) {
129 1
            throw new \RuntimeException('Only one identifier allowed at this time.');
130
        }
131
132 4
        return $metadata->getIdentifierValues($entity)[$metadata->getIdentifierFieldNames()[0]];
133
    }
134
}
135