Completed
Push — master ( 80a801...35d786 )
by Peter
21:35
created

DefaultController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 9.4285
cc 1
eloc 11
nc 1
nop 1
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
14
class DefaultController extends Controller
15
{
16
    /**
17
     * @Route("/", name="homepage")
18
     */
19
    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...
20
    {
21
        $repository = $this->getDoctrine()->getRepository('SymfonySiBlogBundle:Post');
22
23
        $q = $repository->createQueryBuilder('p')
24
            ->orderBy('p.created', 'DESC')
25
            ->setMaxResults(10)
26
            ->getQuery();
27
28
        $posts = $q->getResult();
29
30
        $jsonData = json_decode(file_get_contents('http://knpbundles.com/newest.json'), true);
31
32
        return $this->render('default/index.html.twig', [
33
            'posts' => $posts,
34
            'bundles' => $jsonData['results']
35
        ]);
36
    }
37
38
    /**
39
     * @Route("/copyrights", name="copyrights")
40
     * @param Request $request
41
     * @return \Symfony\Component\HttpFoundation\Response
42
     */
43
    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...
44
    {
45
        return $this->render('default/copyright.html.twig');
46
    }
47
48
    /**
49
     * @Route("/join-us", name="join")
50
     * @param Request $request
51
     * @return \Symfony\Component\HttpFoundation\Response
52
     */
53
    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...
54
    {
55
        return $this->render('default/join.html.twig');
56
    }
57
58
    /**
59
     * @Route("/contact", name="contact")
60
     * @param Request $request
61
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
62
     */
63
    public function contactAction(Request $request)
64
    {
65
        $contact = new Contact();
66
67
        $form = $this->createFormBuilder($contact)
68
            ->add('name', TextType::class)
69
            ->add('email', EmailType::class)
70
            ->add('message', TextareaType::class)
71
            ->add('send', SubmitType::class)
72
            ->getForm();
73
74
        $form->handleRequest($request);
75
76
        if($form->isValid()) {
77
            // send email to admin
78
            $message = \Swift_Message::newInstance()
79
                ->setSubject('Message from Symfony.si')
80
                ->setFrom($contact->getEmail())
81
                ->setTo($this->container->getParameter('symfonysi_admin_email'))
82
                ->setBody(
83
                    $this->renderView(
84
                        'emails/email.txt.twig',
85
                        array(
86
                            'name' => $contact->getName(),
87
                            'email' => $contact->getEmail(),
88
                            'message' => $contact->getMessage()
89
                        )
90
                    )
91
                )
92
            ;
93
            $this->get('mailer')->send($message);
94
95
            return $this->redirect($this->generateUrl('contact_success'));
96
        }
97
98
        return $this->render('default/contact.html.twig', [
99
            'form' => $form->createView(),
100
        ]);
101
    }
102
103
    /**
104
     * @Route("/contact-succeeded", name="contact_success")
105
     * @param Request $request
106
     * @return \Symfony\Component\HttpFoundation\Response
107
     */
108
    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...
109
    {
110
        return $this->render('default/contactSuccess.html.twig');
111
    }
112
113
    /**
114
     * @Route("/contributors", name="contributors")
115
     * @param Request $request
116
     * @return \Symfony\Component\HttpFoundation\Response
117
     */
118
    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...
119
    {
120
        $cache = $this->get('kernel')->getRootDir().'/../var/cache/github-api-cache';
121
        $client = new \Github\Client(
122
            new \Github\HttpClient\CachedHttpClient(['cache_dir' => $cache])
123
        );
124
        $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...
125
        $docsContributors = $client->api('repo')->contributors('symfony-si', 'symfony-docs-sl');
126
        $contributors = [];
127
128
        foreach($srcContributors as $key=>$contributor) {
129
            $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...
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...
130
            $contributors[$contributor['login']] = [
131
                'name'       => ($user['name']) ? $user['name'] : $contributor['login'],
132
                'html_url'   => $user['html_url'],
133
                'avatar_url' => $user['avatar_url'],
134
            ];
135
        }
136
137
        foreach($docsContributors as $key=>$contributor) {
138
            $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...
139
            $contributors[$contributor['login']] = [
140
                'name'       => ($user['name']) ? $user['name'] : $contributor['login'],
141
                'html_url'   => $user['html_url'],
142
                'avatar_url' => $user['avatar_url'],
143
            ];
144
        }
145
146
        return $this->render('default/contributors.html.twig', [
147
            'contributors' => $contributors
148
        ]);
149
    }
150
151
    /**
152
     * @Route("/resources", name="resources")
153
     * @param Request $request
154
     * @return \Symfony\Component\HttpFoundation\Response
155
     */
156
    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...
157
    {
158
        $file = __DIR__.'/../../../resources/README.md';
159
        $content = (file_exists($file)) ? file_get_contents($file) : '<h1>Symfony resources</h1>';
160
        $html = $this->container->get('markdown.parser')->transformMarkdown($content);
161
162
        return $this->render('default/resources.html.twig', ['html' => $html]);
163
    }
164
}
165