Skip to content

Commit 6800516

Browse files
author
Bernd Hoehl
committed
using other indent
1 parent 32e7fb5 commit 6800516

File tree

2 files changed

+127
-127
lines changed

2 files changed

+127
-127
lines changed

src/AspectMock/Intercept/FunctionInjector.php

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class FunctionInjector
55
{
6-
protected $template = <<<EOF
6+
protected $template = <<<EOF
77
<?php
88
namespace {{ns}};
99
if (!function_exists('{{ns}}\{{func}}')) {
@@ -16,7 +16,7 @@ function {{func}}() {
1616
}
1717
EOF;
1818

19-
protected $templateByRefOptional = <<<EOF
19+
protected $templateByRefOptional = <<<EOF
2020
<?php
2121
namespace {{ns}};
2222
if (!function_exists('{{ns}}\{{func}}')) {
@@ -32,94 +32,94 @@ function {{func}}({{arguments}}) {
3232
}
3333
EOF;
3434

35-
protected $namespace;
35+
protected $namespace;
3636

37-
protected $function;
38-
protected $fileName;
37+
protected $function;
38+
protected $fileName;
3939

40-
function __construct($namespace, $function)
41-
{
42-
$this->namespace = $namespace;
43-
$this->function = $function;
44-
$this->placeOptionalAndReferenceFunction($namespace, $function);
45-
$this->place('ns', $this->namespace);
46-
$this->place('func', $this->function);
47-
}
40+
function __construct($namespace, $function)
41+
{
42+
$this->namespace = $namespace;
43+
$this->function = $function;
44+
$this->placeOptionalAndReferenceFunction($namespace, $function);
45+
$this->place('ns', $this->namespace);
46+
$this->place('func', $this->function);
47+
}
4848

49-
public function getParameterDeclaration(\ReflectionParameter $parameter)
50-
{
51-
$text = (string)$parameter;
52-
if (preg_match('@Parameter\s#[0-9]+\s\[\s<(required|optional)>(.*)(\sor NULL)(.*)\s\]@', $text, $match)) {
53-
$text = $match(2).$match[4];
54-
} elseif (preg_match('@Parameter\s#[0-9]+\s\[\s<(required|optional)>\s(.*)\s\]@', $text, $match)) {
55-
$text = $match[2];
56-
} else {
57-
throw new \Exception('reflection api changed. adjust code.');
58-
}
59-
if ($parameter->isOptional()) {
60-
$text .= "=NULL";
61-
}
62-
return $text;
63-
}
49+
public function getParameterDeclaration(\ReflectionParameter $parameter)
50+
{
51+
$text = (string)$parameter;
52+
if (preg_match('@Parameter\s#[0-9]+\s\[\s<(required|optional)>(.*)(\sor NULL)(.*)\s\]@', $text, $match)) {
53+
$text = $match(2).$match[4];
54+
} elseif (preg_match('@Parameter\s#[0-9]+\s\[\s<(required|optional)>\s(.*)\s\]@', $text, $match)) {
55+
$text = $match[2];
56+
} else {
57+
throw new \Exception('reflection api changed. adjust code.');
58+
}
59+
if ($parameter->isOptional()) {
60+
$text .= "=NULL";
61+
}
62+
return $text;
63+
}
6464

65-
public function placeOptionalAndReferenceFunction($namespace, $function)
66-
{
67-
$reflect = new \ReflectionFunction($function);
68-
$parameters = [];
69-
$args = '';
70-
$byRef = false;
71-
$optionals = false;
72-
$names = [];
73-
foreach ($reflect->getParameters() as $parameter) {
74-
$name = '$'.$parameter->getName();
75-
$newname = '$p'.$parameter->getPosition();
76-
$declaration = str_replace($name, $newname, $this->getParameterDeclaration($parameter));
77-
$name = $newname;
78-
if (!$optionals && $parameter->isOptional()) {
79-
$optionals = true;
80-
}
81-
if ($parameter->isPassedByReference()) {
82-
$name = '&'.$name;
83-
$byRef = true;
84-
}
85-
$names[] = $name;
86-
$parameters[$newname] = $declaration;
87-
}
88-
if ($optionals || $byRef) {
89-
$this->template = $this->templateByRefOptional;
90-
$this->place('arguments', join(', ', $parameters));
91-
$code = '';
92-
for ($i = count($parameters); $i > 0; $i--) {
93-
$code .= " case {$i}: \$args = [" . join(', ', $names) . "]; break;\n";
94-
array_pop($names);
95-
}
96-
$this->place('code', $code);
97-
}
98-
}
65+
public function placeOptionalAndReferenceFunction($namespace, $function)
66+
{
67+
$reflect = new \ReflectionFunction($function);
68+
$parameters = [];
69+
$args = '';
70+
$byRef = false;
71+
$optionals = false;
72+
$names = [];
73+
foreach ($reflect->getParameters() as $parameter) {
74+
$name = '$'.$parameter->getName();
75+
$newname = '$p'.$parameter->getPosition();
76+
$declaration = str_replace($name, $newname, $this->getParameterDeclaration($parameter));
77+
$name = $newname;
78+
if (!$optionals && $parameter->isOptional()) {
79+
$optionals = true;
80+
}
81+
if ($parameter->isPassedByReference()) {
82+
$name = '&'.$name;
83+
$byRef = true;
84+
}
85+
$names[] = $name;
86+
$parameters[$newname] = $declaration;
87+
}
88+
if ($optionals || $byRef) {
89+
$this->template = $this->templateByRefOptional;
90+
$this->place('arguments', join(', ', $parameters));
91+
$code = '';
92+
for ($i = count($parameters); $i > 0; $i--) {
93+
$code .= " case {$i}: \$args = [" . join(', ', $names) . "]; break;\n";
94+
array_pop($names);
95+
}
96+
$this->place('code', $code);
97+
}
98+
}
9999

100-
public function save()
101-
{
102-
$this->fileName = tempnam(sys_get_temp_dir(), $this->function);
103-
file_put_contents($this->fileName, $this->template);
104-
}
100+
public function save()
101+
{
102+
$this->fileName = tempnam(sys_get_temp_dir(), $this->function);
103+
file_put_contents($this->fileName, $this->template);
104+
}
105105

106-
public function inject()
107-
{
108-
require_once $this->fileName;
109-
}
106+
public function inject()
107+
{
108+
require_once $this->fileName;
109+
}
110110

111-
public function getFileName()
112-
{
113-
return $this->fileName;
114-
}
111+
public function getFileName()
112+
{
113+
return $this->fileName;
114+
}
115115

116-
public function getPHP()
117-
{
118-
return $this->template;
119-
}
116+
public function getPHP()
117+
{
118+
return $this->template;
119+
}
120120

121-
protected function place($var, $value)
122-
{
123-
$this->template = str_replace("{{{$var}}}", $value, $this->template);
124-
}
121+
protected function place($var, $value)
122+
{
123+
$this->template = str_replace("{{{$var}}}", $value, $this->template);
124+
}
125125
}

tests/unit/FunctionInjectorTest.php

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,59 @@ class FunctionInjectorTest extends \Codeception\TestCase\Test
1111
* @var FunctionInjector
1212
*/
1313
protected $funcInjector;
14-
/**
15-
* @var FunctionInjector
16-
*/
17-
protected $funcOptionalParameterInjector;
18-
/**
19-
* @var FunctionInjector
20-
*/
21-
protected $funcReferencedParameterInjector;
14+
/**
15+
* @var FunctionInjector
16+
*/
17+
protected $funcOptionalParameterInjector;
18+
/**
19+
* @var FunctionInjector
20+
*/
21+
protected $funcReferencedParameterInjector;
2222

2323
public function _before()
2424
{
2525
$this->funcInjector = new FunctionInjector('demo', 'strlen');
2626
$this->funcOptionalParameterInjector = new FunctionInjector('demo', 'explode');
27-
$this->funcReferencedParameterInjector = new FunctionInjector('demo', 'preg_match');
27+
$this->funcReferencedParameterInjector = new FunctionInjector('demo', 'preg_match');
2828
test::clean();
2929
}
3030

3131
public function testTemplate()
3232
{
3333
$php = $this->funcInjector->getPHP();
3434
if ((int)ini_get('mbstring.func_overload') & 2 !== 0) {
35-
// in this case strlen is overloaded by mb_strlen with optional parameter charset
36-
verify($php)->contains("function strlen(\$p0, \$p1=NULL)");
37-
verify($php)->contains("case 2: \$args = [\$p0, \$p1]; break;");
38-
verify($php)->contains("case 1: \$args = [\$p0]; break;");
39-
verify($php)->contains("return call_user_func_array('strlen', \$args);");
40-
} else {
41-
verify($php)->contains("function strlen()");
42-
verify($php)->contains("return call_user_func_array('strlen', func_get_args());");
43-
}
35+
// in this case strlen is overloaded by mb_strlen with optional parameter charset
36+
verify($php)->contains("function strlen(\$p0, \$p1=NULL)");
37+
verify($php)->contains("case 2: \$args = [\$p0, \$p1]; break;");
38+
verify($php)->contains("case 1: \$args = [\$p0]; break;");
39+
verify($php)->contains("return call_user_func_array('strlen', \$args);");
40+
} else {
41+
verify($php)->contains("function strlen()");
42+
verify($php)->contains("return call_user_func_array('strlen', func_get_args());");
43+
}
4444
}
4545

4646
public function testOptionalParameterTemplate()
47-
{
48-
$php = $this->funcOptionalParameterInjector->getPHP();
49-
verify($php)->contains("function explode(\$p0, \$p1, \$p2=NULL)");
50-
verify($php)->contains("case 3: \$args = [\$p0, \$p1, \$p2]; break;");
51-
verify($php)->contains("case 2: \$args = [\$p0, \$p1]; break;");
52-
verify($php)->contains("case 1: \$args = [\$p0]; break;");
53-
verify($php)->contains("return call_user_func_array('explode', \$args);");
54-
}
47+
{
48+
$php = $this->funcOptionalParameterInjector->getPHP();
49+
verify($php)->contains("function explode(\$p0, \$p1, \$p2=NULL)");
50+
verify($php)->contains("case 3: \$args = [\$p0, \$p1, \$p2]; break;");
51+
verify($php)->contains("case 2: \$args = [\$p0, \$p1]; break;");
52+
verify($php)->contains("case 1: \$args = [\$p0]; break;");
53+
verify($php)->contains("return call_user_func_array('explode', \$args);");
54+
}
5555

56-
public function testReferencedParameterTemplate()
57-
{
58-
$php = $this->funcReferencedParameterInjector->getPHP();
59-
verify($php)->contains("function preg_match(\$p0, \$p1, &\$p2=NULL, \$p3=NULL, \$p4=NULL)");
60-
verify($php)->contains("case 5: \$args = [\$p0, \$p1, &\$p2, \$p3, \$p4]; break;");
61-
verify($php)->contains("case 4: \$args = [\$p0, \$p1, &\$p2, \$p3]; break;");
62-
verify($php)->contains("case 3: \$args = [\$p0, \$p1, &\$p2]; break;");
63-
verify($php)->contains("case 2: \$args = [\$p0, \$p1]; break;");
64-
verify($php)->contains("case 1: \$args = [\$p0]; break;");
65-
verify($php)->contains("return call_user_func_array('preg_match', \$args);");
66-
}
56+
public function testReferencedParameterTemplate()
57+
{
58+
$php = $this->funcReferencedParameterInjector->getPHP();
59+
verify($php)->contains("function preg_match(\$p0, \$p1, &\$p2=NULL, \$p3=NULL, \$p4=NULL)");
60+
verify($php)->contains("case 5: \$args = [\$p0, \$p1, &\$p2, \$p3, \$p4]; break;");
61+
verify($php)->contains("case 4: \$args = [\$p0, \$p1, &\$p2, \$p3]; break;");
62+
verify($php)->contains("case 3: \$args = [\$p0, \$p1, &\$p2]; break;");
63+
verify($php)->contains("case 2: \$args = [\$p0, \$p1]; break;");
64+
verify($php)->contains("case 1: \$args = [\$p0]; break;");
65+
verify($php)->contains("return call_user_func_array('preg_match', \$args);");
66+
}
6767

6868
public function testSave()
6969
{
@@ -123,12 +123,12 @@ public function testFailedVerification()
123123
}
124124

125125
public function testReferencedParameter()
126-
{
127-
$func = test::func('\demo', 'preg_match', 10);
128-
expect(preg_match('@[0-9]+@', '1234', $match))->equals(10);
129-
test::clean();
130-
expect(preg_match('@[0-9]+@', '1234#', $match))->equals(1);
131-
expect($match[0])->equals('1234');
132-
}
126+
{
127+
$func = test::func('\demo', 'preg_match', 10);
128+
expect(preg_match('@[0-9]+@', '1234', $match))->equals(10);
129+
test::clean();
130+
expect(preg_match('@[0-9]+@', '1234#', $match))->equals(1);
131+
expect($match[0])->equals('1234');
132+
}
133133

134134
}

0 commit comments

Comments
 (0)