Completed
Push — master ( c15895...5432fb )
by Peter
22:43
created

DefaultController::ecosystemAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\Form\Extension\Core\Type\EmailType;
8
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
9
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
10
use Symfony\Component\Form\Extension\Core\Type\TextType;
11
use Symfony\Component\HttpFoundation\Request;
12
use AppBundle\Entity\Contact;
13
use League\CommonMark\CommonMarkConverter;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
15
16
17
class DefaultController extends Controller
18
{
19
    /**
20
     * @Route("/", name="homepage")
21
     * @Cache(expires="tomorrow", public=true)
22
     */
23
    public function indexAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
    {
25
        $repository = $this->getDoctrine()->getRepository('AppBundle:Post');
26
27
        $q = $repository->createQueryBuilder('p')
28
            ->orderBy('p.created', 'DESC')
29
            ->setMaxResults(10)
30
            ->getQuery();
31
32
        $posts = $q->getResult();
33
34
        $jsonData = json_decode(file_get_contents('http://knpbundles.com/newest.json'), true);
35
36
        return $this->render('default/index.html.twig', [
37
            'posts' => $posts,
38
            'bundles' => $jsonData['results']
39
        ]);
40
    }
41
42
    /**
43
     * @Route("/copyrights", name="copyrights")
44
     * @param Request $request
45
     * @return \Symfony\Component\HttpFoundation\Response
46
     */
47
    public function copyrightAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
    {
49
        return $this->render('default/copyright.html.twig');
50
    }
51
52
    /**
53
     * @Route("/join-us", name="join")
54
     * @param Request $request
55
     * @return \Symfony\Component\HttpFoundation\Response
56
     */
57
    public function joinAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59
        return $this->render('default/join.html.twig');
60
    }
61
62
    /**
63
     * @Route("/contact", name="contact")
64
     * @param Request $request
65
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
66
     */
67
    public function contactAction(Request $request)
68
    {
69
        $contact = new Contact();
70
71
        $form = $this->createFormBuilder($contact)
72
            ->add('name', TextType::class)
73
            ->add('email', EmailType::class)
74
            ->add('message', TextareaType::class)
75
            ->add('send', SubmitType::class)
76
            ->getForm();
77
78
        $form->handleRequest($request);
79
80
        if($form->isValid()) {
81
            // send email to admin
82
            $message = \Swift_Message::newInstance()
83
                ->setSubject('Message from Symfony.si')
84
                ->setFrom($contact->getEmail())
85
                ->setTo($this->container->getParameter('symfonysi_admin_email'))
86
                ->setBody(
87
                    $this->renderView(
88
                        'emails/email.txt.twig',
89
                        array(
90
                            'name' => $contact->getName(),
91
                            'email' => $contact->getEmail(),
92
                            'message' => $contact->getMessage()
93
                        )
94
                    )
95
                )
96
            ;
97
            $this->get('mailer')->send($message);
98
99
            return $this->redirect($this->generateUrl('contact_success'));
100
        }
101
102
        return $this->render('default/contact.html.twig', [
103
            'form' => $form->createView(),
104
        ]);
105
    }
106
107
    /**
108
     * @Route("/contact-succeeded", name="contact_success")
109
     * @param Request $request
110
     * @return \Symfony\Component\HttpFoundation\Response
111
     */
112
    public function contactSuccessAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
    {
114
        return $this->render('default/contactSuccess.html.twig');
115
    }
116
117
    /**
118
     * @Route("/contributors", name="contributors")
119
     * @param Request $request
120
     * @return \Symfony\Component\HttpFoundation\Response
121
     */
122
    public function contributorsAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
123
    {
124
        $cache = $this->get('kernel')->getRootDir().'/../var/cache/github-api-cache';
125
        $client = new \Github\Client(
126
            new \Github\HttpClient\CachedHttpClient(['cache_dir' => $cache])
127
        );
128
        $srcContributors = $client->api('repo')->contributors('symfony-si', 'symfony.si');
0 ignored issues
show
Bug introduced by
The method contributors does only exist in Github\Api\Repo, but not in Github\Api\Authorization...rch and Github\Api\User.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
129
        $docsContributors = $client->api('repo')->contributors('symfony-si', 'symfony-docs-sl');
130
        $contributors = [];
131
132
        foreach($srcContributors as $key=>$contributor) {
133
            $user = $client->api('user')->show($contributor['login']);
0 ignored issues
show
Bug introduced by
The method show does only exist in Github\Api\Authorization...epo and Github\Api\User, but not in Github\Api\Deployment an...t and Github\Api\Search.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Unused Code introduced by
The call to CurrentUser::show() has too many arguments starting with $contributor['login'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
The call to show() misses some required arguments starting with $repository.
Loading history...
Bug introduced by
The call to show() misses a required argument $repository.

This check looks for function calls that miss required arguments.

Loading history...
134
            $contributors[$contributor['login']] = [
135
                'name'       => ($user['name']) ? $user['name'] : $contributor['login'],
136
                'html_url'   => $user['html_url'],
137
                'avatar_url' => $user['avatar_url'],
138
            ];
139
        }
140
141
        foreach($docsContributors as $key=>$contributor) {
142
            $user = $client->api('user')->show($contributor['login']);
0 ignored issues
show
Unused Code introduced by
The call to CurrentUser::show() has too many arguments starting with $contributor['login'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
The call to show() misses some required arguments starting with $repository.
Loading history...
Bug introduced by
The call to show() misses a required argument $repository.

This check looks for function calls that miss required arguments.

Loading history...
143
            $contributors[$contributor['login']] = [
144
                'name'       => ($user['name']) ? $user['name'] : $contributor['login'],
145
                'html_url'   => $user['html_url'],
146
                'avatar_url' => $user['avatar_url'],
147
            ];
148
        }
149
150
        return $this->render('default/contributors.html.twig', [
151
            'contributors' => $contributors
152
        ]);
153
    }
154
155
    /**
156
     * @Route("/resources", name="resources")
157
     * @param Request $request
158
     * @return \Symfony\Component\HttpFoundation\Response
159
     */
160
    public function resourcesAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
161
    {
162
        $file = __DIR__.'/../../../resources/README.md';
163
        $content = (file_exists($file)) ? file_get_contents($file) : '<h1>Symfony resources</h1>';
164
        $html = $this->container->get('markdown.parser')->transformMarkdown($content);
165
166
        return $this->render('default/resources.html.twig', ['html' => $html]);
167
    }
168
169
    /**
170
     * @Route("/cheatsheet", name="cheatsheet")
171
     * @return \Symfony\Component\HttpFoundation\Response
172
     */
173
    public function cheatsheetAction()
174
    {
175
        $file = __DIR__.'/../../../cheatsheet/README.md';
176
        $content = (file_exists($file)) ? file_get_contents($file) : '<h1>Symfony cheat sheet</h1>';
177
178
        $converter = new CommonMarkConverter();
179
        $html = $converter->convertToHtml($content);
180
181
        return $this->render('default/cheatsheet.html.twig', ['html' => $html]);
182
    }
183
184
    /**
185
     * @Route("/ecosystem", name="ecosystem")
186
     * @return \Symfony\Component\HttpFoundation\Response
187
     */
188
    public function ecosystemAction()
189
    {
190
        $projects = $this->getDoctrine()
191
            ->getRepository('AppBundle:Project')
192
            ->findAll();
193
194
        return $this->render(
195
            'default/ecosystem.html.twig',
196
            ['projects' => $projects]
197
        );
198
    }
199
}
200