Completed
Push — master ( 5432fb...a44a55 )
by Peter
22:26
created

DefaultController::ecosystemAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 112
Code Lines 96

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 112
rs 8.2857
cc 1
eloc 96
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use AppBundle\Entity\Contact;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
15
use Symfony\Component\HttpFoundation\Response;
16
use Mni\FrontYAML\Parser;
17
use AppBundle\Entity\Project;
18
19
class DefaultController extends Controller
20
{
21
    /**
22
     * @Route("/", name="homepage")
23
     * @Cache(expires="tomorrow", public=true)
24
     */
25
    public function indexAction()
26
    {
27
        $posts = $this->get('app.repository.post')->findLatest();
28
29
        $jsonData = json_decode(file_get_contents('http://knpbundles.com/newest.json'), true);
30
31
        return $this->render('default/index.html.twig', [
32
            'posts' => $posts,
33
            'bundles' => $jsonData['results']
34
        ]);
35
    }
36
37
    /**
38
     * @Route("/copyrights", name="copyrights")
39
     * @param Request $request
40
     * @return Response
41
     */
42
    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...
43
    {
44
        return $this->render('default/copyright.html.twig');
45
    }
46
47
    /**
48
     * @Route("/join-us", name="join")
49
     * @param Request $request
50
     * @return Response
51
     */
52
    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...
53
    {
54
        return $this->render('default/join.html.twig');
55
    }
56
57
    /**
58
     * @Route("/contact", name="contact")
59
     * @Cache(maxage="20", public=true)
60
     *
61
     * @param Request $request
62
     * @return RedirectResponse|Response
63
     */
64
    public function contactAction(Request $request)
65
    {
66
        $contact = new Contact();
67
68
        $form = $this->createFormBuilder($contact)
69
            ->add('name', TextType::class)
70
            ->add('email', EmailType::class)
71
            ->add('message', TextareaType::class)
72
            ->add('send', SubmitType::class)
73
            ->getForm();
74
75
        $form->handleRequest($request);
76
77
        if($form->isValid()) {
78
            // send email to admin
79
            $message = \Swift_Message::newInstance()
80
                ->setSubject('Message from Symfony.si')
81
                ->setFrom($contact->getEmail())
82
                ->setTo($this->container->getParameter('symfonysi_admin_email'))
83
                ->setBody(
84
                    $this->renderView(
85
                        'emails/email.txt.twig',
86
                        [
87
                            'name' => $contact->getName(),
88
                            'email' => $contact->getEmail(),
89
                            'message' => $contact->getMessage()
90
                        ]
91
                    )
92
                )
93
            ;
94
            $this->get('mailer')->send($message);
95
96
            return $this->redirect($this->generateUrl('contact_success'));
97
        }
98
99
        return $this->render('default/contact.html.twig', [
100
            'form' => $form->createView(),
101
        ]);
102
    }
103
104
    /**
105
     * @Route("/contact-succeeded", name="contact_success")
106
     * @param Request $request
107
     * @return Response
108
     */
109
    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...
110
    {
111
        return $this->render('default/contactSuccess.html.twig');
112
    }
113
114
    /**
115
     * @Route("/contributors", name="contributors")
116
     * @param Request $request
117
     * @return Response
118
     */
119
    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...
120
    {
121
        $cache = $this->get('kernel')->getRootDir().'/../var/cache/github-api-cache';
122
        $client = new \Github\Client(
123
            new \Github\HttpClient\CachedHttpClient(['cache_dir' => $cache])
124
        );
125
        $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...
126
        $docsContributors = $client->api('repo')->contributors('symfony-si', 'symfony-docs-sl');
127
        $contributors = [];
128
129
        foreach($srcContributors as $key=>$contributor) {
130
            $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...
131
            $contributors[$contributor['login']] = [
132
                'name'       => ($user['name']) ? $user['name'] : $contributor['login'],
133
                'html_url'   => $user['html_url'],
134
                'avatar_url' => $user['avatar_url'],
135
            ];
136
        }
137
138
        foreach($docsContributors as $key=>$contributor) {
139
            $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...
140
            $contributors[$contributor['login']] = [
141
                'name'       => ($user['name']) ? $user['name'] : $contributor['login'],
142
                'html_url'   => $user['html_url'],
143
                'avatar_url' => $user['avatar_url'],
144
            ];
145
        }
146
147
        return $this->render('default/contributors.html.twig', [
148
            'contributors' => $contributors
149
        ]);
150
    }
151
152
    /**
153
     * @Route("/resources", name="resources")
154
     * @param Request $request
155
     * @return Response
156
     */
157
    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...
158
    {
159
        $file = $this->get('kernel')->getRootDir().'/../vendor/symfony-si/symfony-resources/README.md';
160
        $content = (file_exists($file)) ? file_get_contents($file) : '<h1>Symfony resources</h1>';
161
        $parser = new Parser();
162
        $document = $parser->parse($content);
163
164
        return $this->render('default/resources.html.twig', ['html' => $document->getContent()]);
165
    }
166
167
    /**
168
     * @Route("/cheatsheet", name="cheatsheet")
169
     * @return Response
170
     */
171
    public function cheatsheetAction()
172
    {
173
        $file = $this->get('kernel')->getRootDir().'/../vendor/symfony-si/symfony-cheatsheet/README.md';
174
        $content = (file_exists($file)) ? file_get_contents($file) : '<h1>Symfony cheat sheet</h1>';
175
        $parser = new Parser();
176
        $document = $parser->parse($content);
177
178
        return $this->render('default/cheatsheet.html.twig', ['html' => $document->getContent()]);
179
    }
180
181
    /**
182
     * @Route("/ecosystem", name="ecosystem")
183
     * @return Response
184
     */
185
    public function ecosystemAction()
186
    {
187
        $projects = [];
188
        $project = new Project();
189
        $project->setTitle('Symfony Framework');
190
        $project->setDescription('Prevod ogrodja Symfony');
191
        $project->setLink('https://github.com/symfony/symfony');
192
        $project->setRepository('https://github.com/symfony/symfony');
193
        $project->setSlug('symfony');
194
        $projects[] = $project;
195
196
        $project = new Project();
197
        $project->setTitle('Symfony.com');
198
        $project->setDescription('Symfony.com website');
199
        $project->setLink('https://github.com/symfony/symfony-marketing');
200
        $project->setRepository('https://github.com/symfony/symfony-marketing');
201
        $project->setSlug('symfony-marketing');
202
        $projects[] = $project;
203
204
        $project = new Project();
205
        $project->setTitle('Sonata Project');
206
        $project->setDescription('Prevod projekta Sonata Project');
207
        $project->setLink('https://github.com/sonata-project');
208
        $project->setRepository('https://github.com/sonata-project');
209
        $project->setSlug('sonata-project');
210
        $projects[] = $project;
211
212
        $project = new Project();
213
        $project->setTitle('EasyAdminBundle');
214
        $project->setDescription('Prevod Symfony paketa EasyAdminBundle');
215
        $project->setLink('https://github.com/javiereguiluz/EasyAdminBundle');
216
        $project->setRepository('https://github.com/javiereguiluz/EasyAdminBundle');
217
        $project->setSlug('easy-admin-bundle');
218
        $projects[] = $project;
219
220
        $project = new Project();
221
        $project->setTitle('PHP: The Right Way');
222
        $project->setDescription('An easy-to-read, quick reference for PHP best practices, accepted coding standards, and links to authoritative tutorials around the Web');
223
        $project->setLink('http://sl.phptherightway.com');
224
        $project->setRepository('https://github.com/symfony-si/php-the-right-way');
225
        $project->setSlug('php-the-right-way');
226
        $projects[] = $project;
227
228
        $project = new Project();
229
        $project->setTitle('PHP FIG');
230
        $project->setDescription('PHP Standards Recommendations');
231
        $project->setLink('http://php-fig.org');
232
        $project->setRepository('https://github.com/php-fig/fig-standards');
233
        $project->setSlug('php-fig-standards');
234
        $projects[] = $project;
235
236
        $project = new Project();
237
        $project->setTitle('Magento');
238
        $project->setDescription('Magento 1.x Translation');
239
        $project->setLink('http://magento.com/');
240
        $project->setRepository('https://github.com/symfony-si/magento1-sl-si');
241
        $project->setSlug('magento1');
242
        $projects[] = $project;
243
244
        $project = new Project();
245
        $project->setTitle('Magento 2');
246
        $project->setDescription('Magento 2.x Translation');
247
        $project->setLink('http://magento.com/');
248
        $project->setRepository('https://github.com/symfony-si/magento2-sl_si');
249
        $project->setSlug('magento2');
250
        $projects[] = $project;
251
252
        $project = new Project();
253
        $project->setTitle('Semver.org');
254
        $project->setDescription('Semantic Versions');
255
        $project->setLink('http://semver.org/lang/sl');
256
        $project->setRepository('https://github.com/mojombo/semver.org');
257
        $project->setSlug('semantic-versioning');
258
        $projects[] = $project;
259
260
        $project = new Project();
261
        $project->setTitle('The PHP League');
262
        $project->setDescription('Slovenski prevod strani PHP lige paketov');
263
        $project->setLink('http://thephpleague.com/sl/');
264
        $project->setRepository('https://github.com/thephpleague/thephpleague.github.io');
265
        $project->setSlug('the-php-league');
266
        $projects[] = $project;
267
268
        $project = new Project();
269
        $project->setTitle('Yii framework');
270
        $project->setDescription('Slovenski prevod ogrodja Yii 2');
271
        $project->setLink('https://github.com/yiisoft/yii2');
272
        $project->setRepository('https://github.com/yiisoft/yii2');
273
        $project->setSlug('the-php-league');
274
        $projects[] = $project;
275
276
        $project = new Project();
277
        $project->setTitle('Progit');
278
        $project->setDescription('Slovenski prevod knjige progit');
279
        $project->setLink('http://git-scm.com/book/sl');
280
        $project->setRepository('https://github.com/progit/progit2-sl');
281
        $project->setSlug('progit');
282
        $projects[] = $project;
283
284
        $project = new Project();
285
        $project->setTitle('Zend Framework 2');
286
        $project->setDescription('Slovenian translation of Zend Framework 2');
287
        $project->setLink('https://github.com/zendframework/zf2');
288
        $project->setRepository('https://github.com/zendframework/zf2');
289
        $project->setSlug('zend-framework-2');
290
        $projects[] = $project;
291
292
        return $this->render(
293
            'default/ecosystem.html.twig',
294
            ['projects' => $projects]
295
        );
296
    }
297
}
298