Skip to content

Commit b9da34f

Browse files
committed
✨ Handle case-insensitive functions
1 parent 151b6fb commit b9da34f

File tree

6 files changed

+46
-5
lines changed

6 files changed

+46
-5
lines changed

bin/run-tests.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ for test_dir in tests/*; do
3737
diff "${representation_file_path}" "${expected_representation_file_path}"
3838

3939
if [ $? -ne 0 ]; then
40+
echo "FAILED"
4041
exit_code=1
4142
fi
4243

4344
echo "${test_dir_name}: comparing mapping.json to expected_mapping.json"
4445
diff "${mapping_file_path}" "${expected_mapping_file_path}"
4546

4647
if [ $? -ne 0 ]; then
48+
echo "FAILED"
4749
exit_code=1
4850
fi
4951
fi

phpunit-tests/FunctionMappingTest.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function helloWorldB()
4848
}
4949
CODE;
5050

51-
$this->assertSameRepresentationWithMapping($codeA, $codeB, '{"fn0":"helloWorldA"}', '{"fn0":"helloWorldB"}');
51+
$this->assertSameRepresentationWithMapping($codeA, $codeB, '{"fn0":"helloworlda"}', '{"fn0":"helloworldb"}');
5252
}
5353

5454
public function testDoesNotRenameCoreFunctions(): void
@@ -64,4 +64,40 @@ public function testDoesNotRenameCoreFunctions(): void
6464
'{}',
6565
);
6666
}
67+
68+
public function testCaseInsensitiveFunctions(): void
69+
{
70+
$code = <<<'CODE'
71+
<?php
72+
function A() {}
73+
a();
74+
CODE;
75+
76+
$this->assertRepresentation(
77+
$code,
78+
<<<'CODE'
79+
function fn0()
80+
{
81+
}
82+
fn0();
83+
CODE,
84+
'{"fn0":"a"}',
85+
);
86+
}
87+
88+
public function testCaseInsensitiveNativeFunctions(): void
89+
{
90+
$code = <<<'CODE'
91+
<?php
92+
FunCtioN_ExiStS();
93+
CODE;
94+
95+
$this->assertRepresentation(
96+
$code,
97+
<<<'CODE'
98+
function_exists();
99+
CODE,
100+
'{}',
101+
);
102+
}
67103
}

phpunit-tests/HelloWorldTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ function fn0()
2929
}
3030
EOF
3131
, $result);
32-
$this->assertEquals('{"fn0":"helloWorld"}', $mapping->toJson());
32+
$this->assertEquals('{"fn0":"helloworld"}', $mapping->toJson());
3333
}
3434
}

phpunit-tests/StripCommentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function fn0()
2929
return "Hello World!";
3030
}
3131
EOF,
32-
'{"fn0":"helloWorld"}',
32+
'{"fn0":"helloworld"}',
3333
);
3434
}
3535
}

phpunit-tests/VariableMappingTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ function helloWorld()
3131
$this->assertSameRepresentationWithMapping(
3232
$codeA,
3333
$codeB,
34-
'{"fn0":"helloWorld","v0":"a"}',
35-
'{"fn0":"helloWorld","v0":"b"}',
34+
'{"fn0":"helloworld","v0":"a"}',
35+
'{"fn0":"helloworld","v0":"b"}',
3636
);
3737
}
3838
}

src/Mapping.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use function get_defined_functions;
1212
use function json_encode;
1313
use function ksort;
14+
use function mb_strtolower;
1415

1516
use const JSON_FORCE_OBJECT;
1617
use const JSON_THROW_ON_ERROR;
@@ -51,6 +52,8 @@ public function toJson(): string
5152

5253
public function addFunction(string $name): string
5354
{
55+
// TRANSFORM: Function names are case-insensitive in PHP
56+
$name = mb_strtolower($name);
5457
// Do not rename built-in functions, functions_exists() includes user-defined functions
5558
if (array_key_exists($name, $this->internalFunctions)) {
5659
return $name;

0 commit comments

Comments
 (0)