GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 5f320d...cb2cf0 )
by Bruno
13:55
created

YumlClient   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 96.43%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 101
ccs 27
cts 28
cp 0.9643
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A makeDslText() 0 4 1
A getGraphUrl() 0 8 1
A downloadImage() 0 7 2
A getMetadata() 0 4 1
A getClasses() 0 11 2
A generateGraph() 0 6 1
1
<?php
2
3
namespace Onurb\Bundle\YumlBundle\Yuml;
4
5
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\Mapping\ClassMetadataFactory;
8
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory as ClassMetadataFactoryInterface;
9
use Onurb\Bundle\YumlBundle\Curl\Curl;
10
use Onurb\Bundle\YumlBundle\Curl\CurlInterface;
11
use Onurb\Doctrine\ORMMetadataGrapher\YUMLMetadataGrapher as MetadataGrapher;
12
use Onurb\Doctrine\ORMMetadataGrapher\YUMLMetadataGrapherInterface as MetadataGrapherInterface;
13
14
/**
15
 * Utility to generate Yuml compatible strings from metadata graphs
16
 * Adaptation of DoctrineORMModule\Yuml\YumlController for ZendFramework-Zend-Developer-Tools
17
 *
18
 * @license MIT
19
 * @link    http://www.doctrine-project.org/
20
 * @author  Marco Pivetta <[email protected]>
21
 * @author  Bruno Heron <[email protected]>
22
 **/
23
class YumlClient implements YumlClientInterface
24
{
25
    const YUML_POST_URL = 'https://yuml.me/diagram/plain/class';
26
    const YUML_REDIRECT_URL = 'https://yuml.me/';
27
28
    protected $entityManager;
29
30
    protected $metadataFactory;
31
32
    protected $metadataGrapher;
33
34
    /**
35
     * @param EntityManagerInterface $entityManager
36
     * @param ClassMetadataFactoryInterface|null $classMetadataFactory
37
     * @param MetadataGrapherInterface|null $metadataGrapher
38
     */
39 4
    public function __construct(
40
        EntityManagerInterface          $entityManager,
41
        ClassMetadataFactoryInterface   $classMetadataFactory = null,
42
        MetadataGrapherInterface        $metadataGrapher = null
43
    ) {
44 4
        $this->entityManager = $entityManager;
45 4
        $this->metadataFactory = $classMetadataFactory ? $classMetadataFactory : new ClassMetadataFactory();
46 4
        $this->metadataFactory->setEntityManager($this->entityManager);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persiste...ng\ClassMetadataFactory as the method setEntityManager() does only exist in the following implementations of said interface: Doctrine\ORM\Mapping\ClassMetadataFactory, Doctrine\ORM\Tools\Disco...tedClassMetadataFactory.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
47 4
        $this->metadataGrapher = $metadataGrapher ? $metadataGrapher : new MetadataGrapher();
48 4
    }
49
50
    /**
51
     * Get doctrine metadata as yuml.
52
     *
53
     * @return string
54
     */
55 1
    public function makeDslText()
56
    {
57 1
        return $this->generateGraph($this->getClasses());
58
    }
59
60
    /**
61
     * Use yuml.me to generate an image from yuml.
62
     *
63
     * @param string $dsl_text
64
     *
65
     * @return string The url of the generated image.
66
     */
67 1
    public function getGraphUrl($dsl_text)
68
    {
69 1
        $curl = new Curl(self::YUML_POST_URL);
70 1
        $curl->setPosts(array('dsl_text' => $dsl_text));
71 1
        $return = $curl->getResponse();
72
73 1
        return self::YUML_REDIRECT_URL . $return;
74
    }
75
76
    /**
77
     * @param string $graphUrl
78
     * @param string $filename
79
     * @return mixed
80
     */
81 1
    public function downloadImage($graphUrl, $filename, CurlInterface $curl = null)
82
    {
83 1
        $curl = $curl ? $curl : new Curl($graphUrl);
84 1
        $curl->setOutput($filename);
85
86 1
        return $curl->getResponse();
87
    }
88
89
    /**
90
     * @return array
91
     */
92 1
    private function getMetadata()
93
    {
94 1
        return $this->metadataFactory->getAllMetadata();
95
    }
96
97
    /**
98
     * @return array
99
     */
100 1
    private function getClasses()
101
    {
102 1
        $classes = array();
103
        /** @var ClassMetadata $class */
104 1
        foreach ($this->getMetadata() as $class) {
105 1
            $classes[$class->getName()] = $class;
106
        }
107 1
        ksort($classes);
108
109 1
        return $classes;
110
    }
111
112
    /**
113
     * @param $classes
114
     *
115
     * @return string
116
     */
117 1
    private function generateGraph($classes)
118
    {
119 1
        $graph = $this->metadataGrapher->generateFromMetadata($classes);
0 ignored issues
show
Bug introduced by
The call to generateFromMetadata() misses a required argument $displayTypes.

This check looks for function calls that miss required arguments.

Loading history...
120
121
        return $graph;
122
    }
123
}
124