Skip to content

Commit 3398316

Browse files
committed
Code cleanup (PHPMD, PHPDoc, removed unused functions, etc)
1 parent 6e777c9 commit 3398316

File tree

3 files changed

+135
-145
lines changed

3 files changed

+135
-145
lines changed

Symfony/src/Codebender/BuilderBundle/Controller/DefaultController.php

Lines changed: 76 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ public function statusAction()
2929
* Includes several checks in order to ensure the validity of the data provided as well
3030
* as authentication.
3131
*
32-
* @param $auth_key
32+
* @param $authKey
3333
* @param $version
3434
* @return Response
3535
*/
36-
public function handleRequestAction($auth_key, $version)
36+
public function handleRequestAction($authKey, $version)
3737
{
38-
if ($auth_key !== $this->container->getParameter('auth_key'))
38+
if ($authKey !== $this->container->getParameter('auth_key'))
3939
{
4040
return new Response(json_encode(array("success" => false, "message" => "Invalid authorization key.")));
4141
}
@@ -83,39 +83,38 @@ public function handleRequestAction($auth_key, $version)
8383
*/
8484
protected function compile($contents)
8585
{
86-
$apihandler = $this->get('codebender_builder.handler');
86+
$apiHandler = $this->get('codebender_builder.handler');
8787

8888
$files = $contents["files"];
8989

90-
$this->checkForUserProject($files);
90+
$this->checkForUserIdProjectId($files);
9191

92-
$userLibs = array();
92+
$userLibraries = array();
9393

94-
if (array_key_exists('libraries', $contents))
95-
$userLibs = $contents['libraries'];
94+
if (array_key_exists('libraries', $contents)) {
95+
$userLibraries = $contents['libraries'];
96+
}
9697

97-
$parsedLibs = $this->checkHeaders($files, $userLibs);
98+
$userAndLibmanLibraries = $this->returnProvidedAndFetchedLibraries($files, $userLibraries);
9899

99-
$contents["libraries"] = $parsedLibs['libraries'];
100+
$contents["libraries"] = $userAndLibmanLibraries['libraries'];
100101

101-
$request_content = json_encode($contents);
102+
$compilerRequestContent = json_encode($contents);
102103

103104
// perform the actual post to the compiler
104-
$data = $apihandler->post_raw_data($this->container->getParameter('compiler'), $request_content);
105+
$data = $apiHandler->postRawData($this->container->getParameter('compiler'), $compilerRequestContent);
105106

106-
$decoded = json_decode($data, true);
107-
if ($decoded === NULL)
108-
{
107+
$decodedResponse = json_decode($data, true);
108+
if ($decodedResponse === NULL) {
109109
return json_encode(array("success" => false, "message"=> "Failed to get compiler response."));
110110
}
111111

112-
if ($decoded["success"] === false && !array_key_exists("step", $decoded))
113-
{
114-
$decoded["step"] = "unknown";
112+
if ($decodedResponse["success"] === false && !array_key_exists("step", $decodedResponse)) {
113+
$decodedResponse["step"] = "unknown";
115114
}
116115

117-
unset($parsedLibs['libraries']);
118-
$decoded['additionalCode'] = $parsedLibs;
116+
unset($userAndLibmanLibraries['libraries']);
117+
$decoded['additionalCode'] = $userAndLibmanLibraries;
119118

120119
return json_encode($decoded);
121120
}
@@ -134,90 +133,98 @@ protected function getLibraryInfo($data)
134133

135134
$libraryManager = $this->container->getParameter('library');
136135

137-
return $handler->post_raw_data($libraryManager, $data);
136+
return $handler->postRawData($libraryManager, $data);
138137
}
139138

140139
/**
141140
*
142-
* @param array $files
143-
* @param array $userLibs
141+
* @param array $projectFiles
142+
* @param array $userLibraries
144143
*
145144
* @return array
146145
*/
147-
protected function checkHeaders($files, $userLibs)
146+
protected function returnProvidedAndFetchedLibraries($projectFiles, $userLibraries)
148147
{
149148
$apiHandler = $this->get('codebender_builder.handler');
150149

151-
$headers = $apiHandler->read_libraries($files);
150+
$detectedHeaders = $apiHandler->readLibraries($projectFiles);
152151

153152
// declare arrays
154-
$libraries = $notFoundHeaders = $foundHeaders = $fetchedLibs = $providedLibs = array();
155-
156-
$providedLibs = array_keys($userLibs);
157-
158-
$libraries = $userLibs;
159-
160-
foreach ($headers as $header) {
161-
162-
$exists_in_request = false;
163-
foreach ($userLibs as $lib){
164-
foreach ($lib as $libcontent){
165-
if ($libcontent["filename"] == $header.".h") {
166-
$exists_in_request = true;
153+
$notFoundHeaders = array();
154+
$foundHeaders = array();
155+
$librariesFromLibman = array();
156+
$providedLibraries = array_keys($userLibraries);
157+
$libraries = $userLibraries;
158+
159+
foreach ($detectedHeaders as $header) {
160+
161+
$existsInRequest = false;
162+
foreach ($userLibraries as $library) {
163+
foreach ($library as $libraryContent) {
164+
if ($libraryContent["filename"] == $header.".h") {
165+
$existsInRequest = true;
167166
$foundHeaders[] = $header . ".h";
168167
}
169168
}
170169
}
171-
if ($exists_in_request === false) {
172-
173-
$requestContent = array("type" => "fetch", "library" => $header);
174-
$data = $this->getLibraryInfo(json_encode($requestContent));
175-
$data = json_decode($data, true);
176-
177-
if ($data["success"]) {
178-
$fetchedLibs[] = $header;
179-
$files_to_add = array();
180-
foreach ($data["files"] as $file){
181-
if (in_array(pathinfo($file['filename'], PATHINFO_EXTENSION), array('cpp', 'h', 'c', 'S', 'inc')))
182-
$files_to_add[] = $file;
183-
}
184170

185-
$libraries[$header] = $files_to_add;
186-
$foundHeaders[] = $header . ".h";
171+
if ($existsInRequest === true) {
172+
continue;
173+
}
174+
$requestContent = array("type" => "fetch", "library" => $header);
175+
$data = $this->getLibraryInfo(json_encode($requestContent));
176+
$data = json_decode($data, true);
177+
178+
if ($data['success'] === false) {
179+
$notFoundHeaders[] = $header . ".h";
180+
continue;
181+
}
187182

188-
} elseif (!$data['success']){
189-
$notFoundHeaders[] = $header . ".h";
190-
}
183+
$foundHeaders[] = $header . ".h";
184+
$librariesFromLibman[] = $header;
185+
$filesToBeAdded = array();
186+
foreach ($data["files"] as $file) {
187+
if (in_array(pathinfo($file['filename'], PATHINFO_EXTENSION), array('cpp', 'h', 'c', 'S', 'inc')))
188+
$filesToBeAdded[] = $file;
191189
}
190+
$libraries[$header] = $filesToBeAdded;
192191
}
193192

194193
return array(
195194
'libraries' => $libraries,
196-
'providedLibraries' => $providedLibs,
197-
'fetchedLibraries' => $fetchedLibs,
198-
'detectedHeaders'=> $headers,
195+
'providedLibraries' => $providedLibraries,
196+
'fetchedLibraries' => $librariesFromLibman,
197+
'detectedHeaders'=> $detectedHeaders,
199198
'foundHeaders' => $foundHeaders,
200199
'notFoundHeaders' => $notFoundHeaders);
201200
}
202201

203202
/**
204-
*
205-
* @param array $files
206203
* Checks if project id and user id txt files exist in the request files.
207204
* If not, creates these files with null id
208-
*
205+
*
206+
* @param array $projectFiles
209207
*/
210-
protected function checkForUserProject(&$files)
208+
protected function checkForUserIdProjectId(&$projectFiles)
211209
{
212-
$foundProj = $foundUsr = false;
210+
$foundProject = false;
211+
$foundUser = false;
213212

214-
foreach ($files as $file) {
215-
if (preg_match('/(?<=user_)[\d]+/', $file['filename'])) $foundUsr = true;
216-
if (preg_match('/(?<=project_)[\d]+/', $file['filename'])) $foundProj = true;
213+
foreach ($projectFiles as $file) {
214+
if (preg_match('/(?<=user_)[\d]+/', $file['filename'])) {
215+
$foundUser = true;
216+
}
217+
if (preg_match('/(?<=project_)[\d]+/', $file['filename'])) {
218+
$foundProject = true;
219+
}
217220
}
218221

219-
if (!$foundUsr) $files[] = array('filename' => 'user_null.txt', 'content' => '');
220-
if (!$foundProj) $files[] = array('filename' => 'project_null.txt', 'content' => '');
222+
if (!$foundUser) {
223+
$projectFiles[] = array('filename' => 'user_null.txt', 'content' => '');
224+
}
225+
if (!$foundProject) {
226+
$projectFiles[] = array('filename' => 'project_null.txt', 'content' => '');
227+
}
221228
}
222229
}
223230

Symfony/src/Codebender/BuilderBundle/Handler/DefaultHandler.php

Lines changed: 58 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,24 @@
44

55
class DefaultHandler
66
{
7-
public function get_data($url, $var, $value)
8-
{
9-
$curlHandle = curl_init();
10-
$timeout = 10;
11-
curl_setopt($curlHandle,CURLOPT_URL,$url);
12-
curl_setopt($curlHandle,CURLOPT_RETURNTRANSFER,1);
13-
curl_setopt($curlHandle,CURLOPT_CONNECTTIMEOUT,$timeout);
14-
15-
curl_setopt($curlHandle,CURLOPT_POST,1);
16-
curl_setopt($curlHandle,CURLOPT_POSTFIELDS,$var.'='.$value);
177

18-
$data = curl_exec($curlHandle);
19-
curl_close($curlHandle);
20-
return $data;
21-
}
22-
23-
public function post_raw_data($url, $raw_data)
24-
{
8+
/**
9+
* Uses PHP's curl in order to perform a POST request
10+
* to the given URL with the provided data.
11+
*
12+
* @param $url
13+
* @param $rawData
14+
* @return mixed
15+
*/
16+
public function postRawData($url, $rawData) {
2517
$curlHandle = curl_init();
2618
$timeout = 10;
2719
curl_setopt($curlHandle, CURLOPT_URL,$url);
2820
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
2921
curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, $timeout);
3022

3123
curl_setopt($curlHandle, CURLOPT_POST, 1);
32-
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $raw_data);
24+
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $rawData);
3325
curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, false);
3426

3527
$data = curl_exec($curlHandle);
@@ -38,73 +30,64 @@ public function post_raw_data($url, $raw_data)
3830
return $data;
3931
}
4032

41-
public function get($url)
42-
{
43-
$curlHandle = curl_init();
44-
$timeout = 10;
45-
curl_setopt($curlHandle,CURLOPT_URL,$url);
46-
curl_setopt($curlHandle,CURLOPT_RETURNTRANSFER,1);
47-
curl_setopt($curlHandle,CURLOPT_CONNECTTIMEOUT,$timeout);
48-
49-
$data = curl_exec($curlHandle);
50-
curl_close($curlHandle);
51-
return $data;
52-
}
53-
54-
/**
55-
\brief Extracts included headers from source code.
56-
57-
\param string $code The program's source code.
58-
\return An array of headers.
33+
/**
34+
* Extracts included headers from source code.
35+
* Takes a string containing the source code of a C/C++ program, parses the
36+
* preprocessor directives and makes a list of header files to include. The
37+
* postfix <b>.h</b> is removed from the header names.
38+
*
39+
* @param array $code The program's source code
40+
* @return array An array of headers
41+
*/
42+
function detectHeadersInFile($code) {
43+
/*
44+
* Matches preprocessor include directives, has high tolerance to
45+
* spaces. The actual header (without the postfix .h) is stored in
46+
* register 1.
47+
*
48+
* Examples:
49+
* #include<stdio.h>
50+
* # include "proto.h"
51+
*
52+
*/
53+
$arrowsRegex = "/^\s*#\s*include\s*<\s*([a-zA-Z0-9_+]*)\.h\s*>/";
54+
$quotesRegex = "/^\s*#\s*include\s*\"\s*([a-zA-Z0-9_+]*)\.h\s*\"/";
5955

60-
Takes a string containing the source code of a C/C++ program, parses the
61-
preprocessor directives and makes a list of header files to include. The
62-
postfix <b>.h</b> is removed from the header names.
63-
*/
64-
function read_headers($code)
65-
{
66-
// Matches preprocessor include directives, has high tolerance to
67-
// spaces. The actual header (without the postfix .h) is stored in
68-
// register 1.
69-
//
70-
// Examples:
71-
// #include<stdio.h>
72-
// # include "proto.h"
73-
$REGEX_ARROWS = "/^\s*#\s*include\s*<\s*([a-zA-Z0-9_+]*)\.h\s*>/";
74-
$REGEX_QUOTES = "/^\s*#\s*include\s*\"\s*([a-zA-Z0-9_+]*)\.h\s*\"/";
75-
76-
$headers = array("arrows" => array(), "quotes" => array());
77-
foreach (explode("\n", $code) as $line)
78-
{
79-
if (preg_match($REGEX_ARROWS, $line, $matches))
80-
$headers["arrows"][] = $matches[1];
81-
if (preg_match($REGEX_QUOTES, $line, $matches))
82-
$headers["quotes"][] = $matches[1];
83-
}
56+
$headers = array("arrows" => array(), "quotes" => array());
57+
foreach (explode("\n", $code) as $line)
58+
{
59+
if (preg_match($arrowsRegex, $line, $matches))
60+
$headers["arrows"][] = $matches[1];
61+
if (preg_match($quotesRegex, $line, $matches))
62+
$headers["quotes"][] = $matches[1];
63+
}
8464

85-
$headers["arrows"] = array_unique($headers["arrows"]);
86-
$headers["quotes"] = array_unique($headers["quotes"]);
65+
$headers["arrows"] = array_unique($headers["arrows"]);
66+
$headers["quotes"] = array_unique($headers["quotes"]);
8767

88-
return $headers;
89-
}
68+
return $headers;
69+
}
9070

91-
function read_libraries($sketch_files)
92-
{
71+
/**
72+
* Detects the .ino file in the project files, then calls
73+
* detectHeadersInFile and returns the headers detected in the
74+
* Arduino code.
75+
*
76+
* @param $sketchFiles
77+
* @return array
78+
*/
79+
function readLibraries($sketchFiles) {
9380
// Scan files for headers and locate the corresponding include paths.
9481
$headers = array("arrows" => array(), "quotes" => array());
9582

96-
foreach ($sketch_files as $file)
83+
foreach ($sketchFiles as $file)
9784
{
98-
if (strrpos($file["filename"], ".ino") == strlen($file["filename"]) - 4 &&
99-
strrpos($file["filename"], ".ino") !== false)
100-
{
85+
if (pathinfo($file['filename'], PATHINFO_EXTENSION) === 'ino') {
10186
$code = $file["content"];
102-
$headers = $this->read_headers($code);
87+
$headers = $this->detectHeadersInFile($code);
10388

104-
foreach ($headers["quotes"] as $key => $header)
105-
{
106-
foreach ($sketch_files as $file)
107-
{
89+
foreach ($headers["quotes"] as $key => $header) {
90+
foreach ($sketchFiles as $file) {
10891
if ($file["filename"] == $header.".h")
10992
unset($headers["quotes"][$key]);
11093
}

Symfony/src/Codebender/BuilderBundle/Resources/config/routing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CodebenderBuilderBundle_status_check:
33
defaults: { _controller: CodebenderBuilderBundle:Default:status }
44

55
CodebenderBuilderBundle_handle_request:
6-
pattern: /{auth_key}/{version}
6+
pattern: /{authKey}/{version}
77
defaults: { _controller: CodebenderBuilderBundle:Default:handleRequest }
88
requirements:
99
_method: POST

0 commit comments

Comments
 (0)