Conditions | 8 |
Paths | 12 |
Total Lines | 56 |
Lines | 15 |
Ratio | 26.79 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
68 | public function background( $files = array() ) { |
||
69 | /* |
||
70 | Call this with arguments: |
||
71 | $files = array( |
||
72 | "pdf" => $pdfData, |
||
73 | "background" => $backgroundPdf |
||
74 | ); |
||
75 | */ |
||
76 | if (!sizeof($files)) { |
||
77 | return false; |
||
78 | } |
||
79 | |||
80 | $inputs = array(); |
||
81 | $i = 1; |
||
82 | View Code Duplication | foreach ($files as $key => $file) { |
|
83 | $tempFile = tempnam( $this->config['temp'], 'pdftk-input-' ); |
||
84 | if ( !$tempFile ) { |
||
85 | return ar_error::raiseError( "pdftk: could not create a temporary input file", 202 ); |
||
86 | } |
||
87 | $inputs[$key] = $tempFile; |
||
88 | file_put_contents($tempFile, $file); |
||
89 | } |
||
90 | unset($files); |
||
91 | |||
92 | $outputFile = tempnam( $this->config['temp'], 'pdftk-output-' ); |
||
93 | if ( !$outputFile ) { |
||
94 | return ar_error::raiseError( "pdftk: could not create a temporary output file", 204 ); |
||
95 | } |
||
96 | |||
97 | // pdftk in1.pdf in2.pdf cat output out1.pdf |
||
98 | // system("pdftk.exe \"$frontpage\" background $frontpage_file output \"$wm_frontpage\""); |
||
99 | $execString = $this->config['cmd']; |
||
100 | $execString .= " " . $inputs["pdf"]; |
||
101 | $execString .= " background " . $inputs["background"] . " output $outputFile"; |
||
102 | |||
103 | $execOutput = array(); |
||
104 | $execResult = 0; |
||
105 | |||
106 | exec( $execString, $execOutput, $execResult ); |
||
107 | |||
108 | View Code Duplication | if ( $execResult != 0 ) { |
|
109 | foreach ($inputs as $file) { |
||
110 | @unlink($file); |
||
111 | } |
||
112 | @unlink($outputFile); |
||
113 | return ar_error::raiseError( "pdftk: error ($execResult) while trying to generate PDF: " . implode( "\n", (array) $execOutput ), 203 ); |
||
114 | } |
||
115 | |||
116 | $result = file_get_contents( $outputFile ); |
||
117 | |||
118 | foreach ($inputs as $file) { |
||
119 | @unlink($file); |
||
120 | } |
||
121 | @unlink($outputFile); |
||
122 | return $result; |
||
123 | } |
||
124 | |||
203 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.