create: new folder

This commit is contained in:
abiyasa05 2024-12-31 11:09:29 +07:00
parent 4700d88ab3
commit 1bc5d02894
1704 changed files with 140687 additions and 1 deletions

BIN
Laporan Skripsi.docx Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
PPT Semhas.pptx Normal file

Binary file not shown.

View File

@ -0,0 +1,74 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Email</title>
<style>
.error {
color: red;
}
</style>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body class="container-md my-3">
<?php
//Nilai variabel error
if (isset($_GET['error'])) {
$error = $_GET['error'];
} else {
$error = "";
}
//Pesan kesalahan
$pesan = "";
if ($error == "variable_undefined") {
$pesan = "Sorry, you must access this page from formEmail.php";
} elseif ($error == "empty_name") {
$pesan = "Sorry, name field required";
} elseif ($error == "invalid_name") {
$pesan = "Sorry, name must contains letter";
} elseif ($error == "empty_email") {
$pesan = "Sorry, email field required";
}
if ($error == "invalid_email") {
$pesan = "Sorry, invalid email";
}
//Isian form jika terjadi kesalahan
if (isset($_GET['yourname']) && isset($_GET['youremail'])) {
$name = $_GET['yourname'];
$email = $_GET['youremail'];
} else {
$name = "";
$email = "";
}
?>
<?php if (!empty($pesan)) : ?>
<div class="alert alert-danger error">
<?php echo $pesan; ?>
</div>
<?php endif; ?>
<form action="prosesFormEmail.php" method="get">
<div class="mb-3">
<label for="name" class="form-label">Your Name:</label>
<input type="text" class="form-control" id="name" name="yourname" value="<?php echo $name; ?>">
</div>
<div class="mb-3">
<label for="email" class="form-label">Your Email:</label>
<input type="email" class="form-control" name="youremail" id="email" value="<?php echo $email; ?>">
</div>
<input type="submit" class="btn btn-primary" name="submit" value="Submit">
</form>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>

View File

@ -0,0 +1,69 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Required</title>
<style>
.error {
color: red;
}
</style>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body class="container-md my-3">
<?php
//Nilai variabel error
if (isset($_GET['error'])) {
$error = $_GET['error'];
} else {
$error = "";
}
//Pesan kesalahan
$pesan = "";
if ($error == "variable_undefined") {
$pesan = "Sorry, you must access this page from formRequired.php";
} elseif ($error == "empty_name") {
$pesan = "Sorry, name field required";
} elseif ($error == "empty_address") {
$pesan = "Sorry, address field required";
}
//Isian form jika terjadi kesalahan
if (isset($_GET['yourname']) && isset($_GET['youraddress'])) {
$name = $_GET['yourname'];
$address = $_GET['youraddress'];
} else {
$name = "";
$address = "";
}
?>
<?php if (!empty($pesan)) : ?>
<div class="alert alert-danger error"><?php echo $pesan; ?></div>
<?php endif; ?>
<form action="prosesFormRequired.php" method="get">
<div class="mb-3">
<label for="name" class="form-label">Your Name:</label>
<input type="text" class="form-control" id="name" name="yourname" value="<?php echo $name; ?>">
</div>
<div class="mb-3">
<label for="address" class="form-label">Your Address:</label>
<input type="text" class="form-control" id="address" name="youraddress" value="<?php echo $address; ?>">
</div>
<input type="submit" class="btn btn-primary" name="submit" value="Submit">
</form>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>

View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Handling</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body class="container-md my-3">
<form action="getFormHandling.php" method="get">
<div class="mb-3">
<label for="inputName" class="form-label">Your Name: </label>
<input type="text" class="form-control" name="yourName" id="inputName" placeholder="Input Your Name">
</div>
<div class="mb-3">
<label for="inputAddress" class="form-label">Your Address: </label>
<input type="text" class="form-control" name="yourAddress" id="inputAddress" placeholder="Input Your Address">
</div>
<input class="btn btn-primary" type="submit">
</form>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>

View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Get Form Handling</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body class="container-md my-3 text-center">
<h3>Welcome!!</h3>
<p>
<b><?php echo $_GET["yourName"]; ?></b> from
<b><?php echo $_GET["yourAddress"]; ?></b>
</p>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>

View File

@ -0,0 +1,2 @@
<?php
echo ("Hello World!");

View File

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Form Handling</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body class="container-md my-3">
<form action="postFormHandling.php" method="post">
<div class="mb-3">
<label class="form-label" for="inputName">Your Name: </label>
<input type="text" class="form-control" name="yourName" id="inputName" placeholder="Input Your Name" />
</div>
<div class="mb-3">
<label class="form-label" for="inputAddress">Your Address: </label>
<input type="text" class="form-control" name="yourAddress" id="inputAddress" placeholder="Input Your Address"/>
</div>
<input class="btn btn-primary" type="submit" />
</form>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>

View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Post Form Handling</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body class="container-md my-3 text-center">
<h3>Welcome!!</h3>
<p>
<b><?php echo $_POST["yourName"]; ?></b> from
<b><?php echo $_POST["yourAddress"]; ?></b>
</p>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>

View File

@ -0,0 +1,19 @@
<?php
if (isset($_GET['yourname']) && isset($_GET['youremail'])) {
$name = $_GET['yourname'];
$email = $_GET['youremail'];
$form_input = "&yourname = $name &youremail = $email";
} else {
header("Location:formEmail.php?error=variable_undefined");
}
if (empty($name)) {
header("Location:formEmail.php?error=empty_name" . $form_input);
} elseif (!preg_match("/^[a-zA-z ]*$/", $name)) {
header("Location:formEmail.php?error=invalid_name" . $form_input);
} elseif (empty($email)) {
header("Location:formEmail.php?error=empty_email" . $form_input);
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location:formEmail.php?error=invalid_email" . $form_input);
} else {
echo "Your Name: $name <br> Your Email: $email";
}

View File

@ -0,0 +1,15 @@
<?php
if (isset($_GET['yourname']) && isset($_GET['youraddress'])) {
$name = $_GET['yourname'];
$address = $_GET['youraddress'];
$form_input = "&yourname = $name &youraddress = $address";
} else {
header("Location:formRequired.php?error=variable_undefined");
}
if (empty($name)) {
header("Location:formRequired.php?error=empty_name" . $form_input);
} elseif (empty($address)) {
header("Location:formRequired.php?error=empty_address" . $form_input);
} else {
echo "Your Name: $name <br> Your Address: $address";
}

View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Validasi Form</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body class="container-md my-3">
<form action="validasiForm.php" method="get">
<div class="mb-3">
<label class="form-label" for="inputName">Your Name: </label>
<input class="form-control" type="text" name="yourName" id="inputName" placeholder="Input Your Name"/>
</div>
<div class="mb-3">
<label class="form-label" for="inputAddress">Your Address: </label>
<input class="form-control" type="text" name="yourAddress" id="inputAddress" placeholder="Input Your Address"/>
</div>
<input class="btn btn-primary" type="submit" />
</form>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>

View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Validasi Form</title>
</head>
<body class="container-md my-3 text-center">
<?php
if (isset($_GET["yourName"]) && isset($_GET["yourAddress"])) {
echo "<p>Welcome " . $_GET["yourName"] . " from " . $_GET["yourAddress"] . "";
} else {
echo "Sorry, you must access this page from validasiForm.html";
}
?>
</body>
</html>

View File

@ -0,0 +1,13 @@
{
"name": "aliyyaps/form-testing",
"require-dev": {
"phpunit/phpunit": "^10.5"
},
"authors": [
{
"name": "aliyyaps",
"email": "aliyyaputris23@gmail.com"
}
],
"require": {}
}

1643
form-testing/composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

28
form-testing/phpunit.xml Normal file
View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="false"
beStrictAboutCoverageMetadata="false"
beStrictAboutOutputDuringTests="true"
failOnRisky="true"
failOnWarning="true"
displayDetailsOnTestsThatTriggerWarnings="true"
colors="true">
<!-- Specify your testsuites -->
<testsuites>
<testsuite name="tests">
<directory>tests</directory>
</testsuite>
</testsuites>
<!-- Specify source directories -->
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
failOnRisky="true"
failOnWarning="true"
colors="true">
<testsuites>
<testsuite name="tests">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>

View File

@ -0,0 +1 @@
T1mx0M/poVSHamt78uk+SnhzRzQ0UGgvOVdwMnBlK3dXbGdOTUgyTjlhRFVUOVVPalM1TjZRY2NaVmxEcGZub3haMTNQUmlzRXlqdXFIc2hqaVU1b0JZUkE0enFJaVo4Qjd5cDVWS3hxd0JZbVlTbXI0aXJQcUhvVk1QZDU1NDVFVmVDRlBIS0ZRbndhdkNHRGZtQ3ZLS1ZpL2RQN3pQd3JqS0RMenVsdnhUdVllaWJDL3JDcVZ5NW9NbWV5ZndEZTlrc3JVV2FoZFlPakFYOUM0TnlpUHdNMWFtTXpjMDRDOExobzA0RCtTS0swOTlaaWg4dTJKVyt0UmZSMEpLZVJ5akUxMS9KS3A2UmI2TnJNcm1YcmtUMm9YMzFsbEo0MC8xbzdGTTN3ZTYxbyt1RndxeWN6aVBjQWdiQmpqd1J4RE90UUFrUWgxR095VXF5OUJ5aVZDTDUrRkhMY2N0ZitOSTZIKzRsTUpNVkJidmppQlRkVFFaMENCa1hIRmh6NFd6ckJLV3hwSlhoZDFzSGg3Q2FWRVo1aGx1TFpmLzhmalV2azhWOFp1M1IyN1FqSFcxbUt6ZmZTUHdweWtKNm5ZeUhnc3VORnJXejRtTkQ1aXlURTRjejlBeks5cUVkRHNuVXQzOWhjNzBKdUYwVWJpVEx3eUl5Yk9BeWhMZWJ1QVRUeHpJQi96QUw4Y0FEVkpxYkZ5WnJ0M3ZEbC9od0pKcmd0NkY2SFBCUUd6ZmpkeXVNOU9lVm56MStmTVljSDBoT0VvUUU1cVZpRGZHWUhQVW1pWGcrMDhUVGZTZXMzK1d1bXRkQTlWbFh3eFplSU1sY0FkL3E3QUdBMHNHMWsxajBPWXM3SFJWcVNtbk5CUHNtckVwcGVTOG1hUTAzdUlZWDdYdk9oTElVTllvaWJ6eWozYWRQWmhXdlVNQkJpb1NnQlozSVhLZ081UmMvZmtGa25aeVN2d3k4Y3hzbU9uOTY5N0FNQWdmMndmYnNNOTRpN0MzUG1KNGJkNDJLcDY0bDk4VjVRSC9ObHN4VUQzMWJJUWlwaWNKUmV2LzJOank2TjJNOE5NOFhVSWtMQVIvZW94Nll4QTlwakVWSThLUGUwZm9Na1lTcjZ6TDdpZGRRZmFCNE1oVUtlNGVna0tiSFdzdkp1bUZoMW1pczdBVkRxQ0JvVjZTWXNOaTdad3N0VzJFTE00SlN1MlBHMFB5eWZiZmkvOE5vOUFlQ0ZvV3BUemVENjZNZ0tua1pJSGlmcmRtZCtNVktmR3J1cm9RME5GNUNpVmovdkYzRnJNcEJZVDdZdzU4R1N2NDJ0czVVWDUwUllZQldjYS9RTkxOS0p3VjNQNWUza1MwTzk4NFpNem9ybGYrazZIaG1qcFBmanFRaW1KWWFZWE4xNWJXaFFsdmtXZzFGOTdKeTlYZzhaSmFYanVCY1QrQXBjcG5QNzIzSldLQjQ5WmFDWXl2Vk5hWUExd3dxVkxMZ1lselF4UlVVbDFTMS9iSG53Y09DMDRlZzduc1JoVVlRY2ZsU0xxRU5qMWdwMHJyTFFoRWlTTkZ4OUVhRkJRMHptVlRZa1M2dms4UE5mbDlyZXZlMXJPVDNZa2Q3MjRrWktnUHA5RzZQYVg3Q3I3VitzM0o3RmxGT3BmWHp6K3NXMnYwSFJMbG5venRPWWMzaGRtNnNXN09hbVNuMVlMQ3hzeU1NcDJraktkSW5KdGliSC85N1prNVFPT1FiK2xSOTdnV3E5T3huVHFaRUFscUVjYlk4akRBdXF2Ryt0OHh6OHY0Y2o1bUp4M2JWZGt2MVNGRWFWR1lPQ3EvK1ZZeWxNS1cwWnBZK0dkemoxZGVyTDBEMzZ6cUVTY1JtWlI2MlVnWTBLNzFCY0dWTTh4VERDc1BjWTZSNllRa05Hb0F2VWdRZkQ3YktJZ1grRWNMWUExcUVHa1BNSGJWREZwb2IxNWdvbThKaDc5SFA0YmRWRzhxbXNpUEFNWHlFb0pRUVBkRm9vcytOZEkyQURtSDlZZVdETE5KNXhwVWhWbWdxd2E4RnhXODBXZnNSa3AwWGVIUE5QN1JYZHg4a3RQVXFYSE55MVVvZlVTUHNpOVFrbDFYTGJ0dHpVY0VJdi80eGtLeXM2aFA3aXRGNlREWGpYNG82UE1sVVZxQ2VLcWZKVm4yN1BzMjM2M1B6Wk0zLzhLN1dPNStBendnOG9NZTJlNXY1Q2Rhd1pqQ2JNeHpzb0U1Y29RRWx5VG56Rjg0anU1SFdsU1NES3EwUXRkdDAyYmNra3dNOGlCMmY2MEVJRnp1a2l4SjFxMTI5c0ZYYXUza1hacXhsVjdRak9NdnFTNW5yWkI4V0xWZGl5bTRLNVYwTWxuLzFtUlBHTVFYOFJBb0ZIdSsybmZYRi90U3RudjF4YlNUNkJiSk5aeTFrcXdmeGFXYS92V0Rnc0FiWmcvbDdaN3E3c0xvbXhGcGZLOU1jTzF5TjlxVzlwcDRqU0R5R21FeVkwckgzeUpmU3VldlJoNmhJa1AwZkhRYmtLMTdxWTgxUHU1My9WVHVhbVF1dDZ6WVQzcmpybEhvYW5ETDNxT2tFTytNK2IxNllZK2xYUVpuWHE2dXJ0Q0E2cW9kc2NNeVZBMjJCOWVjbndiQStpUG9aR283TmszNTBVVW82NmFSbGtoZVFtaFdldzBEeFA0dFhheEFtcVh1OVFsdStCeUdkRjhvQi9xTGtmSTJ4cFVPcFo3bUQ1SWZXZ0NQL2h4QzBKS1RwQ2pPY3RLaVpqbnVtRjFSS2YzbkQyYXZGNlg5Tnk2MXVZVzAzQjFYWDV6eHdCOG1yVHR3YmhPZWFvTFlzZllQWFNmNHhOdXErSnJpcVd2NTJOWWdRaSt0WmphM0hDRExMbTFqdkgrNTNDSFpXZExEVk5GT08zcEhRL1hhRURSendXZjh4TDdGL3JSbXB5cFZVNWp2eUhMSXBtVEN1eWR1TUFzcHFRSHZITlA4Y3FmZ3pnaGZM

View File

@ -0,0 +1,178 @@
<?php
require_once __DIR__ . '/helper/TestResultsManager.php';
use PHPUnit\Framework\TestCase;
class FormEmailTest extends TestCase
{
private $testResultsManager;
protected function setUp(): void
{
$this->testResultsManager = new TestResultsManager();
}
public function testVariableUndefined()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$_GET['error'] = 'variable_undefined';
ob_start();
include './apps/formEmail.php';
$output = ob_get_clean();
$dom = new DOMDocument();
$dom->loadHTML($output);
$errorMessage = $dom->getElementsByTagName('div')->item(0)->textContent;
$expectedMessage = "Sorry, you must access this page from formEmail.php";
$this->assertStringContainsString($expectedMessage, $errorMessage, 'The error message for the condition $_GET["error"] == "variable_undefined" is out of specification');
$this->verifyFormAttributes($dom);
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
public function testEmptyName()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$_GET['error'] = 'empty_name';
ob_start();
include './apps/formEmail.php';
$output = ob_get_clean();
$dom = new DOMDocument();
$dom->loadHTML($output);
$errorMessage = $dom->getElementsByTagName('div')->item(0)->textContent;
$expectedMessage = "Sorry, name field required";
$this->assertStringContainsString($expectedMessage, $errorMessage, 'The error message for the condition $_GET["error"] == "empty_name" is out of specification');
$this->verifyFormAttributes($dom);
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
public function testInvalidName()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$_GET['error'] = 'invalid_name';
ob_start();
include './apps/formEmail.php';
$output = ob_get_clean();
$dom = new DOMDocument();
$dom->loadHTML($output);
$errorMessage = $dom->getElementsByTagName('div')->item(0)->textContent;
$expectedMessage = "Sorry, name must contains letter";
$this->assertStringContainsString($expectedMessage, $errorMessage, 'The error message for the condition $_GET["error"] == "empty_name" is out of specification');
$this->verifyFormAttributes($dom);
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
public function testEmptyEmail()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$_GET['error'] = 'empty_email';
ob_start();
include './apps/formEmail.php';
$output = ob_get_clean();
$dom = new DOMDocument();
$dom->loadHTML($output);
$errorMessage = $dom->getElementsByTagName('div')->item(0)->textContent;
$expectedMessage = "Sorry, email field required";
$this->assertStringContainsString($expectedMessage, $errorMessage, 'The error message for the condition $_GET["error"] == "empty_email" is out of specification');
$this->verifyFormAttributes($dom);
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
public function testInvalidEmail()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$_GET['error'] = 'invalid_email';
ob_start();
include './apps/formEmail.php';
$output = ob_get_clean();
$dom = new DOMDocument();
$dom->loadHTML($output);
$errorMessage = $dom->getElementsByTagName('div')->item(0)->textContent;
$expectedMessage = "Sorry, invalid email";
$this->assertStringContainsString($expectedMessage, $errorMessage, 'The error message for the condition $_GET["error"] == "empty_email" is out of specification');
$this->verifyFormAttributes($dom);
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
private function verifyFormAttributes(DOMDocument $dom)
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$form = $dom->getElementsByTagName('form')->item(0);
$this->assertEquals('prosesFormEmail.php', $form->attributes->getNamedItem('action')->nodeValue, "The value of the action attribute on the form doesn't match the specifications");
$this->assertEqualsIgnoringCase('GET', $form->attributes->getNamedItem('method')->nodeValue, "The value of the action attribute on the form doesn't match the specifications");
$labelName = $dom->getElementsByTagName('label')->item(0);
$this->assertEquals('Your Name:', $labelName->textContent, "The text content of the first label element in the form doesn't match");
$inputName = $dom->getElementsByTagName('input')->item(0);
$this->assertEquals('text', $inputName->attributes->getNamedItem('type')->nodeValue, "The value of the type attribute on the first input element in the form != text");
$this->assertEquals('yourname', $inputName->attributes->getNamedItem('name')->nodeValue, "The value of the name attribute on the first input element in the form != yourname");
$this->assertEquals('', $inputName->attributes->getNamedItem('value')->nodeValue);
$labelName = $dom->getElementsByTagName('label')->item(1);
$this->assertEquals('Your Email:', $labelName->textContent, "The text content of the third label element in the form does not match");
$inputAddress = $dom->getElementsByTagName('input')->item(1);
$this->assertEquals('email', $inputAddress->attributes->getNamedItem('type')->nodeValue, "The value of the type attribute on the second input element in the form != email");
$this->assertEquals('youremail', $inputAddress->attributes->getNamedItem('name')->nodeValue, "The value of the name attribute in the second input element in the form != youremail");
$this->assertEquals('', $inputAddress->attributes->getNamedItem('value')->nodeValue);
$inputSubmit = $dom->getElementsByTagName('input')->item(2);
$this->assertEquals('submit', $inputSubmit->attributes->getNamedItem('type')->nodeValue, "The value of the type attribute on the third input element in the form != submit");
$this->assertEquals('submit', $inputSubmit->attributes->getNamedItem('name')->nodeValue, "The value of the name attribute in the third input element in the form != submit");
$this->assertEquals('Submit', $inputSubmit->attributes->getNamedItem('value')->nodeValue, "The value attribute value in the third input element in the form != Submit");
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
}

View File

@ -0,0 +1,121 @@
<?php
require_once __DIR__ . '/helper/TestResultsManager.php';
use PHPUnit\Framework\TestCase;
class FormRequiredTest extends TestCase
{
private $testResultsManager;
protected function setUp(): void
{
$this->testResultsManager = new TestResultsManager();
}
public function testVariableUndefined()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$_GET['error'] = 'variable_undefined';
ob_start();
include './apps/formRequired.php';
$output = ob_get_clean();
$dom = new DOMDocument();
$dom->loadHTML($output);
$errorMessage = $dom->getElementsByTagName('div')->item(0)->textContent;
$expectedMessage = "Sorry, you must access this page from formRequired.php";
$this->assertStringContainsString($expectedMessage, $errorMessage, 'The error message for the condition $_GET["error"] == "variable_undefined" is out of specification');
$this->verifyFormAttributes($dom);
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
public function testEmptyName()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$_GET['error'] = 'empty_name';
ob_start();
include './apps/formRequired.php';
$output = ob_get_clean();
$dom = new DOMDocument();
$dom->loadHTML($output);
$errorMessage = $dom->getElementsByTagName('div')->item(0)->textContent;
$expectedMessage = "Sorry, name field required";
$this->assertStringContainsString($expectedMessage, $errorMessage, 'The error message for the condition $_GET["error"] == "empty_name" is out of specification');
$this->verifyFormAttributes($dom);
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
public function testEmptyAddress()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$_GET['error'] = 'empty_address';
ob_start();
include './apps/formRequired.php';
$output = ob_get_clean();
$dom = new DOMDocument();
$dom->loadHTML($output);
$errorMessage = $dom->getElementsByTagName('div')->item(0)->textContent;
$expectedMessage = "Sorry, address field required";
$this->assertStringContainsString($expectedMessage, $errorMessage, 'The error message for the condition $_GET["error"] == "empty_name" is out of specification');
$this->verifyFormAttributes($dom);
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
private function verifyFormAttributes(DOMDocument $dom)
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$form = $dom->getElementsByTagName('form')->item(0);
$this->assertEquals('prosesFormRequired.php', $form->attributes->getNamedItem('action')->nodeValue, 'The value of the action attribute on the form does not match the specifications');
$this->assertEqualsIgnoringCase('GET', $form->attributes->getNamedItem('method')->nodeValue, 'Method attribute values on the form != GET');
$labelName = $dom->getElementsByTagName('label')->item(0);
$this->assertEquals('Your Name:', $labelName->textContent, 'The text content of the first label element in the form should be "Your Name"');
$inputName = $dom->getElementsByTagName('input')->item(0);
$this->assertEquals('text', $inputName->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute on the first input element in the form should be "text"');
$this->assertEquals('yourname', $inputName->attributes->getNamedItem('name')->nodeValue, 'The value of the name attribute on the first input element in the form should be "yourname"');
$this->assertEquals('', $inputName->attributes->getNamedItem('value')->nodeValue);
$labelName = $dom->getElementsByTagName('label')->item(1);
$this->assertEquals('Your Address:', $labelName->textContent, 'The text content of the third label element in the form should be "Your Address"');
$inputAddress = $dom->getElementsByTagName('input')->item(1);
$this->assertEquals('text', $inputAddress->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute on the second input element in the form should be "text"');
$this->assertEquals('youraddress', $inputAddress->attributes->getNamedItem('name')->nodeValue, 'The value of the name attribute in the second input element in the form should be "youraddress"');
$this->assertEquals('', $inputAddress->attributes->getNamedItem('value')->nodeValue);
$inputSubmit = $dom->getElementsByTagName('input')->item(2);
$this->assertEquals('submit', $inputSubmit->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute on the third input element in the form != submit');
$this->assertEquals('submit', $inputSubmit->attributes->getNamedItem('name')->nodeValue, 'The value of the name attribute in the third input element in the form != submit');
$this->assertEquals('Submit', $inputSubmit->attributes->getNamedItem('value')->nodeValue, 'The value attribute value in the third input element in the form != Submit');
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
}

View File

@ -0,0 +1,87 @@
<?php
require_once __DIR__ . '/helper/TestResultsManager.php';
use PHPUnit\Framework\TestCase;
class GetFormHTMLTest extends TestCase
{
private $testResultsManager;
protected function setUp(): void
{
$this->testResultsManager = new TestResultsManager();
}
public function testFormAttributes()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$html = file_get_contents('./apps/getFormHandling.html');
$dom = new DOMDocument();
$dom->loadHTML($html);
$titleElemen = $dom->getElementsByTagName('title')->item(0);
$this->assertEquals('Form Handling', $titleElemen->nodeValue);
$form = $dom->getElementsByTagName('form')->item(0);
$this->assertEquals('getFormHandling.php', $form->attributes->getNamedItem('action')->nodeValue, "The value of the action attribute on the form doesn't match the specifications");
$this->assertEqualsIgnoringCase('GET', $form->attributes->getNamedItem('method')->nodeValue, 'The value of the method attribute on the form should be "GET"');
// label assertion
$labels = $dom->getElementsByTagName('label');
$labelName = null;
$labelAddress = null;
foreach ($labels as $label) {
$for = $label->getAttributeNode('for');
switch ($for->nodeValue) {
case 'inputName':
$labelName = $label;
break;
case 'inputAddress':
$labelAddress = $label;
break;
default:
$this->fail('There are unnecessary labels, label tags with attribute for == ' . $for->nodeValue);
break;
}
}
$this->assertNotNull($labelName, 'Label tag with for == "inputName" attribute not found');
$this->assertNotNull($labelAddress, 'Label tag with for == "inputAddress" attribute not found');
$this->assertEquals('Your Name: ', $labelName->nodeValue, 'Text content of label element with attributes for == "inputName" should be "Your Name: "');
$this->assertEquals('Your Address: ', $labelAddress->nodeValue, 'Text content of label element with attributes for == "inputAddress" should be "Your Address: "');
// input assertion
$inputs = $dom->getElementsByTagName('input');
$inputName = $inputs->item(0);
$inputAddress = $inputs->item(1);
$this->assertNotNull($inputName, 'The first input tag is not found');
$this->assertNotNull($inputAddress, 'The second input tag is not found');
$this->assertEquals('yourName', $inputName->attributes->getNamedItem('name')->nodeValue, 'The value of the name attribute in the first input tag should be "yourName"');
$this->assertEquals('yourAddress', $inputAddress->attributes->getNamedItem('name')->nodeValue, 'The value of the name attribute in the second input tag should be "yourAddress"');
$this->assertEquals('text', $inputName->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute on the input element with name == "yourName" should be "text"');
$this->assertEquals('inputName', $inputName->attributes->getNamedItem('id')->nodeValue, 'The value of the id attribute on the input element with name == "yourName" should be "inputName"');
$this->assertEquals('Input Your Name', $inputName->attributes->getNamedItem('placeholder')->nodeValue, 'The value of the placeholder attribute on the input element with name == "yourName" should be "Input Your Name"');
$this->assertEquals('text', $inputAddress->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute on the input element with name == "yourAddress" should be "text"');
$this->assertEquals('inputAddress', $inputAddress->attributes->getNamedItem('id')->nodeValue, 'The value of the id attribute on the input element with name == "yourAddress" should be "inputAddress"');
$this->assertEquals('Input Your Address', $inputAddress->attributes->getNamedItem('placeholder')->nodeValue, 'The value of the placeholder attribute on the input element with name == "yourAddress" should be "Input Your Address"');
$inputSubmit = $inputs->item(2);
$this->assertNotNull($inputName, 'The third input tag is not found');
$this->assertEquals('submit', $inputSubmit->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute in the third input element in form != submit');
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
}

View File

@ -0,0 +1,45 @@
<?php
require_once __DIR__ . '/helper/TestResultsManager.php';
use PHPUnit\Framework\TestCase;
class GetFormPHPTest extends TestCase
{
private $testResultsManager;
protected function setUp(): void
{
$this->testResultsManager = new TestResultsManager();
}
public function testGetFormHandlingResult()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$_GET['yourName'] = 'AliyyaPS';
$_GET['yourAddress'] = 'Sidoarjo';
ob_start();
include_once './apps/getFormHandling.php';
$output = ob_get_clean();
$dom = new DOMDocument();
$dom->loadHTML($output);
$titleElemen = $dom->getElementsByTagName('title')->item(0);
$this->assertEquals('Get Form Handling', $titleElemen->nodeValue, 'The text content of the title element on the page should be "Get Form Handling"');
$h3Elemen = $dom->getElementsByTagName('h3')->item(0);
$this->assertEquals('Welcome!!', $h3Elemen->nodeValue, 'Text content of the h3 element should be "Welcome!!"');
$pElemen = $dom->getElementsByTagName('p')->item(0);
$this->assertStringContainsString('AliyyaPS from', $pElemen->nodeValue);
$this->assertStringContainsString('Sidoarjo', $pElemen->nodeValue);
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
}

View File

@ -0,0 +1,34 @@
<?php
require_once __DIR__ . '/helper/TestResultsManager.php';
use PHPUnit\Framework\TestCase;
class IndexTest extends TestCase
{
private $testResultsManager;
protected function setUp(): void
{
$this->testResultsManager = new TestResultsManager();
}
public function testFirstPHPCode()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
ob_start();
include_once './apps/index.php';
$output = ob_get_clean();
$dom = new DOMDocument();
$dom->loadHTML($output);
$content = $dom->textContent;
$expectedMessage = "Hello World!";
$this->assertEquals($expectedMessage, $content, 'No text found "Hello World!"');
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
}

View File

@ -0,0 +1,87 @@
<?php
require_once __DIR__ . '/helper/TestResultsManager.php';
use PHPUnit\Framework\TestCase;
class PostFormHTMLTest extends TestCase
{
private $testResultsManager;
protected function setUp(): void
{
$this->testResultsManager = new TestResultsManager();
}
public function testFormAttributes()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$html = file_get_contents('./apps/postFormHandling.html');
$dom = new DOMDocument();
$dom->loadHTML($html);
$titleElemen = $dom->getElementsByTagName('title')->item(0);
$this->assertEquals('Form Handling', $titleElemen->nodeValue);
$form = $dom->getElementsByTagName('form')->item(0);
$this->assertEquals('postFormHandling.php', $form->attributes->getNamedItem('action')->nodeValue, "The value of the action attribute on the form doesn't match the specifications");
$this->assertEqualsIgnoringCase('POST', $form->attributes->getNamedItem('method')->nodeValue,);
// label assertion
$labels = $dom->getElementsByTagName('label');
$labelName = null;
$labelAddress = null;
foreach ($labels as $label) {
$for = $label->getAttributeNode('for');
switch ($for->nodeValue) {
case 'inputName':
$labelName = $label;
break;
case 'inputAddress':
$labelAddress = $label;
break;
default:
$this->fail('There are unnecessary labels, label tag with attributes for == ' . $for->nodeValue);
break;
}
}
$this->assertNotNull($labelName, 'Tag labels with the for attribute == “inputName” not found');
$this->assertNotNull($labelAddress, 'Tag labels with the for attribute == “inputAddress” not found');
$this->assertEquals('Your Name: ', $labelName->nodeValue);
$this->assertEquals('Your Address: ', $labelAddress->nodeValue);
// input assertion
$inputs = $dom->getElementsByTagName('input');
$inputName = $inputs->item(0);
$inputAddress = $inputs->item(1);
$this->assertNotNull($inputName, 'The first input tag is not found');
$this->assertNotNull($inputAddress, 'The second input tag is not found');
$this->assertEquals('yourName', $inputName->attributes->getNamedItem('name')->nodeValue, 'The value of the name attribute in the first input tag should be "yourName"');
$this->assertEquals('yourAddress', $inputAddress->attributes->getNamedItem('name')->nodeValue, 'The value of the name attribute in the second input tag should be "yourAddress"');
$this->assertEquals('text', $inputName->attributes->getNamedItem('type')->nodeValue);
$this->assertEquals('inputName', $inputName->attributes->getNamedItem('id')->nodeValue);
$this->assertEquals('Input Your Name', $inputName->attributes->getNamedItem('placeholder')->nodeValue);
$this->assertEquals('text', $inputAddress->attributes->getNamedItem('type')->nodeValue);
$this->assertEquals('inputAddress', $inputAddress->attributes->getNamedItem('id')->nodeValue);
$this->assertEquals('Input Your Address', $inputAddress->attributes->getNamedItem('placeholder')->nodeValue);
$inputSubmit = $inputs->item(2);
$this->assertNotNull($inputName, 'The third input tag is not found');
$this->assertEquals('submit', $inputSubmit->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute on the third input element in the form != submit');
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
}

View File

@ -0,0 +1,45 @@
<?php
require_once __DIR__ . '/helper/TestResultsManager.php';
use PHPUnit\Framework\TestCase;
class PostFormPHPTest extends TestCase
{
private $testResultsManager;
protected function setUp(): void
{
$this->testResultsManager = new TestResultsManager();
}
public function testPostFormHandlingResult()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$_POST['yourName'] = 'AliyyaPS';
$_POST['yourAddress'] = 'Sidoarjo';
ob_start();
include_once './apps/postFormHandling.php';
$output = ob_get_clean();
$dom = new DOMDocument();
$dom->loadHTML($output);
$titleElemen = $dom->getElementsByTagName('title')->item(0);
$this->assertEquals('Post Form Handling', $titleElemen->nodeValue, 'The text content of the title element on the page should be "Post Form Handling"');
$h3Elemen = $dom->getElementsByTagName('h3')->item(0);
$this->assertEquals('Welcome!!', $h3Elemen->nodeValue, 'Text content of the h3 element should be "Welcome!!"');
$pElemen = $dom->getElementsByTagName('p')->item(0);
$this->assertStringContainsString('AliyyaPS from', $pElemen->nodeValue);
$this->assertStringContainsString('Sidoarjo', $pElemen->nodeValue);
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
}

View File

@ -0,0 +1,39 @@
<?php
require_once __DIR__ . '/helper/TestResultsManager.php';
use PHPUnit\Framework\TestCase;
class ProsesFormEmailTest extends TestCase
{
private $testResultsManager;
protected function setUp(): void
{
$this->testResultsManager = new TestResultsManager();
}
public function testSuccessfulSubmission()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$_GET['yourname'] = 'Aliyya Putri S';
$_GET['youremail'] = 'example@gmail.com';
ob_start();
include './apps/prosesFormEmail.php';
$output = ob_get_clean();
$dom = new DOMDocument();
$dom->loadHTML($output);
$this->assertStringContainsString('Your Name: Aliyya Putri S', $dom->textContent);
$this->assertStringContainsString('Your Email: example@gmail.com', $dom->textContent);
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
}

View File

@ -0,0 +1,40 @@
<?php
require_once __DIR__ . '/helper/TestResultsManager.php';
use PHPUnit\Framework\TestCase;
class ProsesFormRequiredTest extends TestCase
{
private $testResultsManager;
protected function setUp(): void
{
$this->testResultsManager = new TestResultsManager();
}
public function testSuccessfulSubmission()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$_GET['yourname'] = 'AliyyaPS';
$_GET['youraddress'] = 'Sidoarjo';
ob_start();
include_once './apps/prosesFormRequired.php';
$output = ob_get_clean();
$dom = new DOMDocument();
$dom->loadHTML($output);
$this->assertStringContainsString('Your Name: AliyyaPS', $dom->textContent);
$this->assertStringContainsString('Your Address: Sidoarjo', $dom->textContent);
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
}

View File

@ -0,0 +1,72 @@
<?php
require_once __DIR__ . '/helper/TestResultsManager.php';
use PHPUnit\Framework\TestCase;
class ValidasiFormHTMLTest extends TestCase
{
private $testResultsManager;
protected function setUp(): void
{
$this->testResultsManager = new TestResultsManager();
}
public function testFormAttributes()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$html = file_get_contents('./apps/validasiForm.html');
$dom = new DOMDocument();
$dom->loadHTML($html);
$titleElemen = $dom->getElementsByTagName('title')->item(0);
$this->assertEquals('Validasi Form', $titleElemen->nodeValue);
$form = $dom->getElementsByTagName('form')->item(0);
$this->assertEquals('validasiForm.php', $form->attributes->getNamedItem('action')->nodeValue, 'The value of the action attribute on the form does not match the specifications');
$this->assertEqualsIgnoringCase('GET', $form->attributes->getNamedItem('method')->nodeValue, 'The value of the method attribute on the form should be "GET"');
// label assertion
$labels = $dom->getElementsByTagName('label');
$labelName = $labels->item(0);
$labelAddress = $labels->item(1);
$this->assertEquals('inputName', $labelName->attributes->getNamedItem('for')->nodeValue, 'The value of the for attribute in the first label tag should be "inputName"');
$this->assertEquals('inputAddress', $labelAddress->attributes->getNamedItem('for')->nodeValue, 'The value of the name attribute in the second label tag should be "inputAddress"');
$this->assertEquals('Your Name: ', $labelName->nodeValue, 'Text content of label element with attributes for == "inputName" should be "Your Name: "');
$this->assertEquals('Your Address: ', $labelAddress->nodeValue, 'Text content of label element with attributes for == "inputAddress" should be "Your Address: "');
// input assertion
$inputs = $dom->getElementsByTagName('input');
$inputName = $inputs->item(0);
$inputAddress = $inputs->item(1);
$this->assertNotNull($inputName, 'The first input tag is not found');
$this->assertNotNull($inputAddress, 'The second input tag is not found');
$this->assertEquals('yourName', $inputName->attributes->getNamedItem('name')->nodeValue, 'The value of the name attribute in the first input tag should be "yourName"');
$this->assertEquals('yourAddress', $inputAddress->attributes->getNamedItem('name')->nodeValue, 'The value of the name attribute in the second input tag should be "yourAddress"');
$this->assertEquals('text', $inputName->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute on the input element with name == "yourName" should be "text"');
$this->assertEquals('inputName', $inputName->attributes->getNamedItem('id')->nodeValue, 'The value of the id attribute on the input element with name == "yourName" should be "inputName"');
$this->assertEquals('Input Your Name', $inputName->attributes->getNamedItem('placeholder')->nodeValue, 'The value of the placeholder attribute on the input element with name == "yourName" should be "Input Your Name"');
$this->assertEquals('text', $inputAddress->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute on the input element with name == "yourAddress" should be "text"');
$this->assertEquals('inputAddress', $inputAddress->attributes->getNamedItem('id')->nodeValue, 'The value of the id attribute on the input element with name == "yourAddress" should be "inputAddress"');
$this->assertEquals('Input Your Address', $inputAddress->attributes->getNamedItem('placeholder')->nodeValue, 'The value of the placeholder attribute on the input element with name == "yourAddress" should be "Input Your Address"');
$inputSubmit = $inputs->item(2);
$this->assertNotNull($inputName, 'The third input tag is not found');
$this->assertEquals('submit', $inputSubmit->attributes->getNamedItem('type')->nodeValue, 'The value of the type attribute in the third input element in form != submit');
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
}

View File

@ -0,0 +1,62 @@
<?php
require_once __DIR__ . '/helper/TestResultsManager.php';
use PHPUnit\Framework\TestCase;
class ValidasiFormPHPTest extends TestCase
{
private $testResultsManager;
protected function setUp(): void
{
$this->testResultsManager = new TestResultsManager();
}
public function testValidFormData()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$_GET['yourName'] = 'AliyyaPS';
$_GET['yourAddress'] = 'Sidoarjo';
ob_start();
include_once './apps/validasiForm.php';
$output = ob_get_clean();
$dom = new DOMDocument();
$dom->loadHTML($output);
$titleElemen = $dom->getElementsByTagName('title')->item(0);
$this->assertEquals('Validasi Form', $titleElemen->nodeValue, 'The text content of the title element on the page should be "Validasi Form""');
$bodyElemen = $dom->getElementsByTagName('body')->item(0);
$this->assertStringContainsString('Welcome AliyyaPS from Sidoarjo', $bodyElemen->nodeValue);
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
public function testInvalidFormData()
{
try {
$testCase = get_class($this) . '.' . __FUNCTION__;
$html = file_get_contents('./apps/validasiForm.php');
$dom = new DOMDocument();
$dom->loadHTML($html);
$titleElemen = $dom->getElementsByTagName('title')->item(0);
$this->assertEquals('Validasi Form', $titleElemen->nodeValue, 'The text content of the title element on the page should be "Validasi Form""');
$bodyElemen = $dom->getElementsByTagName('body')->item(0);
$this->assertStringContainsString('Sorry, you must access this page from validasiForm.html', $bodyElemen->nodeValue);
$this->testResultsManager->showTestResult($testCase);
} catch (\Throwable $e) {
$this->testResultsManager->showTestResult($testCase, $e);
throw $e;
}
}
}

View File

@ -0,0 +1,67 @@
<?php
class Encription
{
private $chiper = 'aes-256-cbc';
private $key;
public function __construct()
{
$this->key = $this->getKey();
}
public function encryptData($data)
{
$ivLength = openssl_cipher_iv_length($this->chiper);
$iv = openssl_random_pseudo_bytes($ivLength);
$encrypted = openssl_encrypt($data, $this->chiper, $this->key, 0, $iv);
return base64_encode($iv . $encrypted);
}
public function saveEncryptedJson($filename, $json)
{
$encryptedJson = $this->encryptData($json);
file_put_contents($filename, $encryptedJson);
}
public function decryptData($data)
{
$data = base64_decode($data);
$ivLength = openssl_cipher_iv_length($this->chiper);
$iv = substr($data, 0, $ivLength);
$encrypted = substr($data, $ivLength);
return openssl_decrypt($encrypted, $this->chiper, $this->key, 0, $iv);
}
public function loadDecryptedJson($filename)
{
$encryptedJson = file_get_contents($filename);
return $this->decryptData($encryptedJson);
}
private function getKey()
{
$filePath = './composer.json';
if (!file_exists($filePath)) {
throw "File not found: $filePath";
}
$jsonContent = file_get_contents($filePath);
$composerData = json_decode($jsonContent, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw "Error decoding JSON: " . json_last_error_msg();
}
return $composerData['name'] ?? 'default-key';
}
}

View File

@ -0,0 +1,71 @@
<?php
require_once __DIR__ . '/Encription.php';
class TestResultsManager
{
private $encriptor;
private $resultsFile = './test_results';
public function __construct()
{
$this->encriptor = new Encription();
}
private function loadAllTestResults()
{
if (file_exists($this->resultsFile)) {
$decryptedJson = $this->encriptor->loadDecryptedJson($this->resultsFile);
return json_decode($decryptedJson, true);
}
return [];
}
private function loadTestResults($testName)
{
$allResults = $this->loadAllTestResults();
return $allResults[$testName] ?? [];
}
private function saveTestResults($testName, $results)
{
$allResults = $this->loadAllTestResults();
$allResults[$testName] = $results;
$this->encriptor->saveEncryptedJson($this->resultsFile, json_encode($allResults));
}
private function getImprovementNum($testCase)
{
$savedResult = $this->loadTestResults($testCase);
return $savedResult['improvement'] ?? 0;
}
private function saveCurrentTestResults($testCase, $testResults)
{
$savedResult = $this->loadTestResults($testCase);
$lem = $savedResult['LEM'] ?? null;
if ($lem != $testResults['LEM'] && $lem != null) {
$testResults['improvement']++;
}
$this->saveTestResults($testCase, $testResults);
fwrite(STDOUT, "\n" . "Perbaikan " . $testCase . " : " . $testResults['improvement'] . "\n");
}
public function showTestResult($testCase, $exception = null): void
{
$testResults = [
'status' => $exception == null ? 'passed' : 'failed',
'LEM' => $exception == null ? '' : $exception->getTraceAsString(),
'improvement' => $this->getImprovementNum($testCase),
];
$this->saveCurrentTestResults($testCase, $testResults);
}
}

7
form-testing/vendor/autoload.php vendored Normal file
View File

@ -0,0 +1,7 @@
<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit2408e549752840289b3d15bacee149d3::getLoader();

View File

@ -0,0 +1 @@
{"version":1,"defects":{"PostFormHTMLTest::testFormAttributes":7,"PostFormPHPTest::testPostFormHandlingResult":7,"ValidasiFormPHPTest::testValidFormData":7,"ValidasiFormPHPTest::testInvalidFormData":8,"FormRequiredTest::testVariableUndefined":7,"FormRequiredTest::testEmptyName":7,"FormRequiredTest::testSuccess":5,"FormRequiredTest::testEmptyAddress":7,"ProsesFormRequiredTest::testSuccessfulSubmission":8,"ProsesFormRequiredTest::testVariableUndefined":5,"FormEmailTest::testEmptyName":7,"ProsesFormEmailTest::testEmptyName":8,"ProsesFormEmailTest::testSuccessfulSubmission":7,"ProsesFormEmailTest::testProcessFormEmailRedirectsWithUndefinedVariables":7},"times":{"PostFormHTMLTest::testFormAttributes":0.006,"PostFormPHPTest::testPostFormHandlingResult":0,"GetFormHTMLTest::testFormAttributes":0.002,"GetFormPHPTest::testGetFormHandlingResult":0.001,"ValidasiFormHTMLTest::testFormAttributes":0,"ValidasiFormPHPTest::testValidFormData":0,"ValidasiFormPHPTest::testInvalidFormData":0,"FormRequiredTest::testVariableUndefined":0,"FormRequiredTest::testEmptyName":0,"FormRequiredTest::testEmptyAddress":0,"FormRequiredTest::testSuccess":0.009,"ProsesFormRequiredTest::testSuccessfulSubmission":0,"ProsesFormRequiredTest::testVariableUndefined":0,"FormEmailTest::testVariableUndefined":0.003,"FormEmailTest::testEmptyName":0,"FormEmailTest::testEmptyEmail":0,"FormEmailTest::testInvalidEmail":0,"FormEmailTest::testInvalidName":0,"ProsesFormEmailTest::testSuccessfulSubmission":0.001,"ProsesFormEmailTest::testEmptyName":0.007,"ProsesFormEmailTest::testProcessFormEmailRedirectsWithUndefinedVariables":0.01,"IndexTest::testFirstPHPCode":0}}

117
form-testing/vendor/bin/php-parse vendored Normal file
View File

@ -0,0 +1,117 @@
#!/usr/bin/env php
<?php
/**
* Proxy PHP file generated by Composer
*
* This file includes the referenced bin path (../nikic/php-parser/bin/php-parse)
* using a stream wrapper to prevent the shebang from being output on PHP<8
*
* @generated
*/
namespace Composer;
$GLOBALS['_composer_bin_dir'] = __DIR__;
$GLOBALS['_composer_autoload_path'] = __DIR__ . '/..'.'/autoload.php';
if (PHP_VERSION_ID < 80000) {
if (!class_exists('Composer\BinProxyWrapper')) {
/**
* @internal
*/
final class BinProxyWrapper
{
private $handle;
private $position;
private $realpath;
public function stream_open($path, $mode, $options, &$opened_path)
{
// get rid of phpvfscomposer:// prefix for __FILE__ & __DIR__ resolution
$opened_path = substr($path, 17);
$this->realpath = realpath($opened_path) ?: $opened_path;
$opened_path = $this->realpath;
$this->handle = fopen($this->realpath, $mode);
$this->position = 0;
return (bool) $this->handle;
}
public function stream_read($count)
{
$data = fread($this->handle, $count);
if ($this->position === 0) {
$data = preg_replace('{^#!.*\r?\n}', '', $data);
}
$this->position += strlen($data);
return $data;
}
public function stream_cast($castAs)
{
return $this->handle;
}
public function stream_close()
{
fclose($this->handle);
}
public function stream_lock($operation)
{
return $operation ? flock($this->handle, $operation) : true;
}
public function stream_seek($offset, $whence)
{
if (0 === fseek($this->handle, $offset, $whence)) {
$this->position = ftell($this->handle);
return true;
}
return false;
}
public function stream_tell()
{
return $this->position;
}
public function stream_eof()
{
return feof($this->handle);
}
public function stream_stat()
{
return array();
}
public function stream_set_option($option, $arg1, $arg2)
{
return true;
}
public function url_stat($path, $flags)
{
$path = substr($path, 17);
if (file_exists($path)) {
return stat($path);
}
return false;
}
}
}
if (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper')) {
include("phpvfscomposer://" . __DIR__ . '/..'.'/nikic/php-parser/bin/php-parse');
exit(0);
}
}
include __DIR__ . '/..'.'/nikic/php-parser/bin/php-parse';

5
form-testing/vendor/bin/php-parse.bat vendored Normal file
View File

@ -0,0 +1,5 @@
@ECHO OFF
setlocal DISABLEDELAYEDEXPANSION
SET BIN_TARGET=%~dp0/php-parse
SET COMPOSER_RUNTIME_BIN_DIR=%~dp0
php "%BIN_TARGET%" %*

120
form-testing/vendor/bin/phpunit vendored Normal file
View File

@ -0,0 +1,120 @@
#!/usr/bin/env php
<?php
/**
* Proxy PHP file generated by Composer
*
* This file includes the referenced bin path (../phpunit/phpunit/phpunit)
* using a stream wrapper to prevent the shebang from being output on PHP<8
*
* @generated
*/
namespace Composer;
$GLOBALS['_composer_bin_dir'] = __DIR__;
$GLOBALS['_composer_autoload_path'] = __DIR__ . '/..'.'/autoload.php';
$GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'] = $GLOBALS['__PHPUNIT_ISOLATION_BLACKLIST'] = array(realpath(__DIR__ . '/..'.'/phpunit/phpunit/phpunit'));
if (PHP_VERSION_ID < 80000) {
if (!class_exists('Composer\BinProxyWrapper')) {
/**
* @internal
*/
final class BinProxyWrapper
{
private $handle;
private $position;
private $realpath;
public function stream_open($path, $mode, $options, &$opened_path)
{
// get rid of phpvfscomposer:// prefix for __FILE__ & __DIR__ resolution
$opened_path = substr($path, 17);
$this->realpath = realpath($opened_path) ?: $opened_path;
$opened_path = 'phpvfscomposer://'.$this->realpath;
$this->handle = fopen($this->realpath, $mode);
$this->position = 0;
return (bool) $this->handle;
}
public function stream_read($count)
{
$data = fread($this->handle, $count);
if ($this->position === 0) {
$data = preg_replace('{^#!.*\r?\n}', '', $data);
}
$data = str_replace('__DIR__', var_export(dirname($this->realpath), true), $data);
$data = str_replace('__FILE__', var_export($this->realpath, true), $data);
$this->position += strlen($data);
return $data;
}
public function stream_cast($castAs)
{
return $this->handle;
}
public function stream_close()
{
fclose($this->handle);
}
public function stream_lock($operation)
{
return $operation ? flock($this->handle, $operation) : true;
}
public function stream_seek($offset, $whence)
{
if (0 === fseek($this->handle, $offset, $whence)) {
$this->position = ftell($this->handle);
return true;
}
return false;
}
public function stream_tell()
{
return $this->position;
}
public function stream_eof()
{
return feof($this->handle);
}
public function stream_stat()
{
return array();
}
public function stream_set_option($option, $arg1, $arg2)
{
return true;
}
public function url_stat($path, $flags)
{
$path = substr($path, 17);
if (file_exists($path)) {
return stat($path);
}
return false;
}
}
}
if (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper')) {
include("phpvfscomposer://" . __DIR__ . '/..'.'/phpunit/phpunit/phpunit');
exit(0);
}
}
include __DIR__ . '/..'.'/phpunit/phpunit/phpunit';

5
form-testing/vendor/bin/phpunit.bat vendored Normal file
View File

@ -0,0 +1,5 @@
@ECHO OFF
setlocal DISABLEDELAYEDEXPANSION
SET BIN_TARGET=%~dp0/phpunit
SET COMPOSER_RUNTIME_BIN_DIR=%~dp0
php "%BIN_TARGET%" %*

View File

@ -0,0 +1,572 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Autoload;
/**
* ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
*
* $loader = new \Composer\Autoload\ClassLoader();
*
* // register classes with namespaces
* $loader->add('Symfony\Component', __DIR__.'/component');
* $loader->add('Symfony', __DIR__.'/framework');
*
* // activate the autoloader
* $loader->register();
*
* // to enable searching the include path (eg. for PEAR packages)
* $loader->setUseIncludePath(true);
*
* In this example, if you try to use a class in the Symfony\Component
* namespace or one of its children (Symfony\Component\Console for instance),
* the autoloader will first look for the class under the component/
* directory, and it will then fallback to the framework/ directory if not
* found before giving up.
*
* This class is loosely based on the Symfony UniversalClassLoader.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
* @see https://www.php-fig.org/psr/psr-0/
* @see https://www.php-fig.org/psr/psr-4/
*/
class ClassLoader
{
/** @var ?string */
private $vendorDir;
// PSR-4
/**
* @var array[]
* @psalm-var array<string, array<string, int>>
*/
private $prefixLengthsPsr4 = array();
/**
* @var array[]
* @psalm-var array<string, array<int, string>>
*/
private $prefixDirsPsr4 = array();
/**
* @var array[]
* @psalm-var array<string, string>
*/
private $fallbackDirsPsr4 = array();
// PSR-0
/**
* @var array[]
* @psalm-var array<string, array<string, string[]>>
*/
private $prefixesPsr0 = array();
/**
* @var array[]
* @psalm-var array<string, string>
*/
private $fallbackDirsPsr0 = array();
/** @var bool */
private $useIncludePath = false;
/**
* @var string[]
* @psalm-var array<string, string>
*/
private $classMap = array();
/** @var bool */
private $classMapAuthoritative = false;
/**
* @var bool[]
* @psalm-var array<string, bool>
*/
private $missingClasses = array();
/** @var ?string */
private $apcuPrefix;
/**
* @var self[]
*/
private static $registeredLoaders = array();
/**
* @param ?string $vendorDir
*/
public function __construct($vendorDir = null)
{
$this->vendorDir = $vendorDir;
}
/**
* @return string[]
*/
public function getPrefixes()
{
if (!empty($this->prefixesPsr0)) {
return call_user_func_array('array_merge', array_values($this->prefixesPsr0));
}
return array();
}
/**
* @return array[]
* @psalm-return array<string, array<int, string>>
*/
public function getPrefixesPsr4()
{
return $this->prefixDirsPsr4;
}
/**
* @return array[]
* @psalm-return array<string, string>
*/
public function getFallbackDirs()
{
return $this->fallbackDirsPsr0;
}
/**
* @return array[]
* @psalm-return array<string, string>
*/
public function getFallbackDirsPsr4()
{
return $this->fallbackDirsPsr4;
}
/**
* @return string[] Array of classname => path
* @psalm-return array<string, string>
*/
public function getClassMap()
{
return $this->classMap;
}
/**
* @param string[] $classMap Class to filename map
* @psalm-param array<string, string> $classMap
*
* @return void
*/
public function addClassMap(array $classMap)
{
if ($this->classMap) {
$this->classMap = array_merge($this->classMap, $classMap);
} else {
$this->classMap = $classMap;
}
}
/**
* Registers a set of PSR-0 directories for a given prefix, either
* appending or prepending to the ones previously set for this prefix.
*
* @param string $prefix The prefix
* @param string[]|string $paths The PSR-0 root directories
* @param bool $prepend Whether to prepend the directories
*
* @return void
*/
public function add($prefix, $paths, $prepend = false)
{
if (!$prefix) {
if ($prepend) {
$this->fallbackDirsPsr0 = array_merge(
(array) $paths,
$this->fallbackDirsPsr0
);
} else {
$this->fallbackDirsPsr0 = array_merge(
$this->fallbackDirsPsr0,
(array) $paths
);
}
return;
}
$first = $prefix[0];
if (!isset($this->prefixesPsr0[$first][$prefix])) {
$this->prefixesPsr0[$first][$prefix] = (array) $paths;
return;
}
if ($prepend) {
$this->prefixesPsr0[$first][$prefix] = array_merge(
(array) $paths,
$this->prefixesPsr0[$first][$prefix]
);
} else {
$this->prefixesPsr0[$first][$prefix] = array_merge(
$this->prefixesPsr0[$first][$prefix],
(array) $paths
);
}
}
/**
* Registers a set of PSR-4 directories for a given namespace, either
* appending or prepending to the ones previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param string[]|string $paths The PSR-4 base directories
* @param bool $prepend Whether to prepend the directories
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function addPsr4($prefix, $paths, $prepend = false)
{
if (!$prefix) {
// Register directories for the root namespace.
if ($prepend) {
$this->fallbackDirsPsr4 = array_merge(
(array) $paths,
$this->fallbackDirsPsr4
);
} else {
$this->fallbackDirsPsr4 = array_merge(
$this->fallbackDirsPsr4,
(array) $paths
);
}
} elseif (!isset($this->prefixDirsPsr4[$prefix])) {
// Register directories for a new namespace.
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
} elseif ($prepend) {
// Prepend directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge(
(array) $paths,
$this->prefixDirsPsr4[$prefix]
);
} else {
// Append directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge(
$this->prefixDirsPsr4[$prefix],
(array) $paths
);
}
}
/**
* Registers a set of PSR-0 directories for a given prefix,
* replacing any others previously set for this prefix.
*
* @param string $prefix The prefix
* @param string[]|string $paths The PSR-0 base directories
*
* @return void
*/
public function set($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr0 = (array) $paths;
} else {
$this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
}
}
/**
* Registers a set of PSR-4 directories for a given namespace,
* replacing any others previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param string[]|string $paths The PSR-4 base directories
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function setPsr4($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr4 = (array) $paths;
} else {
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
}
}
/**
* Turns on searching the include path for class files.
*
* @param bool $useIncludePath
*
* @return void
*/
public function setUseIncludePath($useIncludePath)
{
$this->useIncludePath = $useIncludePath;
}
/**
* Can be used to check if the autoloader uses the include path to check
* for classes.
*
* @return bool
*/
public function getUseIncludePath()
{
return $this->useIncludePath;
}
/**
* Turns off searching the prefix and fallback directories for classes
* that have not been registered with the class map.
*
* @param bool $classMapAuthoritative
*
* @return void
*/
public function setClassMapAuthoritative($classMapAuthoritative)
{
$this->classMapAuthoritative = $classMapAuthoritative;
}
/**
* Should class lookup fail if not found in the current class map?
*
* @return bool
*/
public function isClassMapAuthoritative()
{
return $this->classMapAuthoritative;
}
/**
* APCu prefix to use to cache found/not-found classes, if the extension is enabled.
*
* @param string|null $apcuPrefix
*
* @return void
*/
public function setApcuPrefix($apcuPrefix)
{
$this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
}
/**
* The APCu prefix in use, or null if APCu caching is not enabled.
*
* @return string|null
*/
public function getApcuPrefix()
{
return $this->apcuPrefix;
}
/**
* Registers this instance as an autoloader.
*
* @param bool $prepend Whether to prepend the autoloader or not
*
* @return void
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
if (null === $this->vendorDir) {
return;
}
if ($prepend) {
self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders;
} else {
unset(self::$registeredLoaders[$this->vendorDir]);
self::$registeredLoaders[$this->vendorDir] = $this;
}
}
/**
* Unregisters this instance as an autoloader.
*
* @return void
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
if (null !== $this->vendorDir) {
unset(self::$registeredLoaders[$this->vendorDir]);
}
}
/**
* Loads the given class or interface.
*
* @param string $class The name of the class
* @return true|null True if loaded, null otherwise
*/
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
includeFile($file);
return true;
}
return null;
}
/**
* Finds the path to the file where the class is defined.
*
* @param string $class The name of the class
*
* @return string|false The path if found, false otherwise
*/
public function findFile($class)
{
// class map lookup
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
}
if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
return false;
}
if (null !== $this->apcuPrefix) {
$file = apcu_fetch($this->apcuPrefix.$class, $hit);
if ($hit) {
return $file;
}
}
$file = $this->findFileWithExtension($class, '.php');
// Search for Hack files if we are running on HHVM
if (false === $file && defined('HHVM_VERSION')) {
$file = $this->findFileWithExtension($class, '.hh');
}
if (null !== $this->apcuPrefix) {
apcu_add($this->apcuPrefix.$class, $file);
}
if (false === $file) {
// Remember that this class does not exist.
$this->missingClasses[$class] = true;
}
return $file;
}
/**
* Returns the currently registered loaders indexed by their corresponding vendor directories.
*
* @return self[]
*/
public static function getRegisteredLoaders()
{
return self::$registeredLoaders;
}
/**
* @param string $class
* @param string $ext
* @return string|false
*/
private function findFileWithExtension($class, $ext)
{
// PSR-4 lookup
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
$subPath = $class;
while (false !== $lastPos = strrpos($subPath, '\\')) {
$subPath = substr($subPath, 0, $lastPos);
$search = $subPath . '\\';
if (isset($this->prefixDirsPsr4[$search])) {
$pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
foreach ($this->prefixDirsPsr4[$search] as $dir) {
if (file_exists($file = $dir . $pathEnd)) {
return $file;
}
}
}
}
}
// PSR-4 fallback dirs
foreach ($this->fallbackDirsPsr4 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
return $file;
}
}
// PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
} else {
// PEAR-like class name
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
}
if (isset($this->prefixesPsr0[$first])) {
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
}
}
}
// PSR-0 fallback dirs
foreach ($this->fallbackDirsPsr0 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
// PSR-0 include paths.
if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
return $file;
}
return false;
}
}
/**
* Scope isolated include.
*
* Prevents access to $this/self from included files.
*
* @param string $file
* @return void
* @private
*/
function includeFile($file)
{
include $file;
}

View File

@ -0,0 +1,350 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer;
use Composer\Autoload\ClassLoader;
use Composer\Semver\VersionParser;
/**
* This class is copied in every Composer installed project and available to all
*
* See also https://getcomposer.org/doc/07-runtime.md#installed-versions
*
* To require its presence, you can require `composer-runtime-api ^2.0`
*/
class InstalledVersions
{
/**
* @var mixed[]|null
* @psalm-var array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}|array{}|null
*/
private static $installed;
/**
* @var bool|null
*/
private static $canGetVendors;
/**
* @var array[]
* @psalm-var array<string, array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}>
*/
private static $installedByVendor = array();
/**
* Returns a list of all package names which are present, either by being installed, replaced or provided
*
* @return string[]
* @psalm-return list<string>
*/
public static function getInstalledPackages()
{
$packages = array();
foreach (self::getInstalled() as $installed) {
$packages[] = array_keys($installed['versions']);
}
if (1 === \count($packages)) {
return $packages[0];
}
return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
}
/**
* Returns a list of all package names with a specific type e.g. 'library'
*
* @param string $type
* @return string[]
* @psalm-return list<string>
*/
public static function getInstalledPackagesByType($type)
{
$packagesByType = array();
foreach (self::getInstalled() as $installed) {
foreach ($installed['versions'] as $name => $package) {
if (isset($package['type']) && $package['type'] === $type) {
$packagesByType[] = $name;
}
}
}
return $packagesByType;
}
/**
* Checks whether the given package is installed
*
* This also returns true if the package name is provided or replaced by another package
*
* @param string $packageName
* @param bool $includeDevRequirements
* @return bool
*/
public static function isInstalled($packageName, $includeDevRequirements = true)
{
foreach (self::getInstalled() as $installed) {
if (isset($installed['versions'][$packageName])) {
return $includeDevRequirements || empty($installed['versions'][$packageName]['dev_requirement']);
}
}
return false;
}
/**
* Checks whether the given package satisfies a version constraint
*
* e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call:
*
* Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3')
*
* @param VersionParser $parser Install composer/semver to have access to this class and functionality
* @param string $packageName
* @param string|null $constraint A version constraint to check for, if you pass one you have to make sure composer/semver is required by your package
* @return bool
*/
public static function satisfies(VersionParser $parser, $packageName, $constraint)
{
$constraint = $parser->parseConstraints($constraint);
$provided = $parser->parseConstraints(self::getVersionRanges($packageName));
return $provided->matches($constraint);
}
/**
* Returns a version constraint representing all the range(s) which are installed for a given package
*
* It is easier to use this via isInstalled() with the $constraint argument if you need to check
* whether a given version of a package is installed, and not just whether it exists
*
* @param string $packageName
* @return string Version constraint usable with composer/semver
*/
public static function getVersionRanges($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
$ranges = array();
if (isset($installed['versions'][$packageName]['pretty_version'])) {
$ranges[] = $installed['versions'][$packageName]['pretty_version'];
}
if (array_key_exists('aliases', $installed['versions'][$packageName])) {
$ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
}
if (array_key_exists('replaced', $installed['versions'][$packageName])) {
$ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
}
if (array_key_exists('provided', $installed['versions'][$packageName])) {
$ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
}
return implode(' || ', $ranges);
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @param string $packageName
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present
*/
public static function getVersion($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
if (!isset($installed['versions'][$packageName]['version'])) {
return null;
}
return $installed['versions'][$packageName]['version'];
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @param string $packageName
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present
*/
public static function getPrettyVersion($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
if (!isset($installed['versions'][$packageName]['pretty_version'])) {
return null;
}
return $installed['versions'][$packageName]['pretty_version'];
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @param string $packageName
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference
*/
public static function getReference($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
if (!isset($installed['versions'][$packageName]['reference'])) {
return null;
}
return $installed['versions'][$packageName]['reference'];
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @param string $packageName
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as install path. Packages of type metapackages also have a null install path.
*/
public static function getInstallPath($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null;
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @return array
* @psalm-return array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}
*/
public static function getRootPackage()
{
$installed = self::getInstalled();
return $installed[0]['root'];
}
/**
* Returns the raw installed.php data for custom implementations
*
* @deprecated Use getAllRawData() instead which returns all datasets for all autoloaders present in the process. getRawData only returns the first dataset loaded, which may not be what you expect.
* @return array[]
* @psalm-return array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}
*/
public static function getRawData()
{
@trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED);
if (null === self::$installed) {
// only require the installed.php file if this file is loaded from its dumped location,
// and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
if (substr(__DIR__, -8, 1) !== 'C') {
self::$installed = include __DIR__ . '/installed.php';
} else {
self::$installed = array();
}
}
return self::$installed;
}
/**
* Returns the raw data of all installed.php which are currently loaded for custom implementations
*
* @return array[]
* @psalm-return list<array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}>
*/
public static function getAllRawData()
{
return self::getInstalled();
}
/**
* Lets you reload the static array from another file
*
* This is only useful for complex integrations in which a project needs to use
* this class but then also needs to execute another project's autoloader in process,
* and wants to ensure both projects have access to their version of installed.php.
*
* A typical case would be PHPUnit, where it would need to make sure it reads all
* the data it needs from this class, then call reload() with
* `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure
* the project in which it runs can then also use this class safely, without
* interference between PHPUnit's dependencies and the project's dependencies.
*
* @param array[] $data A vendor/composer/installed.php data set
* @return void
*
* @psalm-param array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>} $data
*/
public static function reload($data)
{
self::$installed = $data;
self::$installedByVendor = array();
}
/**
* @return array[]
* @psalm-return list<array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}>
*/
private static function getInstalled()
{
if (null === self::$canGetVendors) {
self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
}
$installed = array();
if (self::$canGetVendors) {
foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
if (isset(self::$installedByVendor[$vendorDir])) {
$installed[] = self::$installedByVendor[$vendorDir];
} elseif (is_file($vendorDir.'/composer/installed.php')) {
$installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php';
if (null === self::$installed && strtr($vendorDir.'/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) {
self::$installed = $installed[count($installed) - 1];
}
}
}
}
if (null === self::$installed) {
// only require the installed.php file if this file is loaded from its dumped location,
// and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
if (substr(__DIR__, -8, 1) !== 'C') {
self::$installed = require __DIR__ . '/installed.php';
} else {
self::$installed = array();
}
}
$installed[] = self::$installed;
return $installed;
}
}

21
form-testing/vendor/composer/LICENSE vendored Normal file
View File

@ -0,0 +1,21 @@
Copyright (c) Nils Adermann, Jordi Boggiano
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,11 @@
<?php
// autoload_files.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'6124b4c8570aa390c21fafd04a26c69f' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/deep_copy.php',
'ec07570ca5a812141189b1fa81503674' => $vendorDir . '/phpunit/phpunit/src/Framework/Assert/Functions.php',
);

View File

@ -0,0 +1,9 @@
<?php
// autoload_namespaces.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
);

View File

@ -0,0 +1,11 @@
<?php
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'PhpParser\\' => array($vendorDir . '/nikic/php-parser/lib/PhpParser'),
'DeepCopy\\' => array($vendorDir . '/myclabs/deep-copy/src/DeepCopy'),
);

View File

@ -0,0 +1,78 @@
<?php
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit2408e549752840289b3d15bacee149d3
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
/**
* @return \Composer\Autoload\ClassLoader
*/
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit2408e549752840289b3d15bacee149d3', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit2408e549752840289b3d15bacee149d3', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit2408e549752840289b3d15bacee149d3::getInitializer($loader));
} else {
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
}
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit2408e549752840289b3d15bacee149d3::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire2408e549752840289b3d15bacee149d3($fileIdentifier, $file);
}
return $loader;
}
}
/**
* @param string $fileIdentifier
* @param string $file
* @return void
*/
function composerRequire2408e549752840289b3d15bacee149d3($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
require $file;
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,257 @@
<?php return array(
'root' => array(
'pretty_version' => '1.0.0+no-version-set',
'version' => '1.0.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => NULL,
'name' => 'aliyyaps/form-testing',
'dev' => true,
),
'versions' => array(
'aliyyaps/form-testing' => array(
'pretty_version' => '1.0.0+no-version-set',
'version' => '1.0.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => NULL,
'dev_requirement' => false,
),
'myclabs/deep-copy' => array(
'pretty_version' => '1.12.0',
'version' => '1.12.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../myclabs/deep-copy',
'aliases' => array(),
'reference' => '3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c',
'dev_requirement' => true,
),
'nikic/php-parser' => array(
'pretty_version' => 'v5.0.2',
'version' => '5.0.2.0',
'type' => 'library',
'install_path' => __DIR__ . '/../nikic/php-parser',
'aliases' => array(),
'reference' => '139676794dc1e9231bf7bcd123cfc0c99182cb13',
'dev_requirement' => true,
),
'phar-io/manifest' => array(
'pretty_version' => '2.0.4',
'version' => '2.0.4.0',
'type' => 'library',
'install_path' => __DIR__ . '/../phar-io/manifest',
'aliases' => array(),
'reference' => '54750ef60c58e43759730615a392c31c80e23176',
'dev_requirement' => true,
),
'phar-io/version' => array(
'pretty_version' => '3.2.1',
'version' => '3.2.1.0',
'type' => 'library',
'install_path' => __DIR__ . '/../phar-io/version',
'aliases' => array(),
'reference' => '4f7fd7836c6f332bb2933569e566a0d6c4cbed74',
'dev_requirement' => true,
),
'phpunit/php-code-coverage' => array(
'pretty_version' => '10.1.15',
'version' => '10.1.15.0',
'type' => 'library',
'install_path' => __DIR__ . '/../phpunit/php-code-coverage',
'aliases' => array(),
'reference' => '5da8b1728acd1e6ffdf2ff32ffbdfd04307f26ae',
'dev_requirement' => true,
),
'phpunit/php-file-iterator' => array(
'pretty_version' => '4.1.0',
'version' => '4.1.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../phpunit/php-file-iterator',
'aliases' => array(),
'reference' => 'a95037b6d9e608ba092da1b23931e537cadc3c3c',
'dev_requirement' => true,
),
'phpunit/php-invoker' => array(
'pretty_version' => '4.0.0',
'version' => '4.0.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../phpunit/php-invoker',
'aliases' => array(),
'reference' => 'f5e568ba02fa5ba0ddd0f618391d5a9ea50b06d7',
'dev_requirement' => true,
),
'phpunit/php-text-template' => array(
'pretty_version' => '3.0.1',
'version' => '3.0.1.0',
'type' => 'library',
'install_path' => __DIR__ . '/../phpunit/php-text-template',
'aliases' => array(),
'reference' => '0c7b06ff49e3d5072f057eb1fa59258bf287a748',
'dev_requirement' => true,
),
'phpunit/php-timer' => array(
'pretty_version' => '6.0.0',
'version' => '6.0.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../phpunit/php-timer',
'aliases' => array(),
'reference' => 'e2a2d67966e740530f4a3343fe2e030ffdc1161d',
'dev_requirement' => true,
),
'phpunit/phpunit' => array(
'pretty_version' => '10.5.24',
'version' => '10.5.24.0',
'type' => 'library',
'install_path' => __DIR__ . '/../phpunit/phpunit',
'aliases' => array(),
'reference' => '5f124e3e3e561006047b532fd0431bf5bb6b9015',
'dev_requirement' => true,
),
'sebastian/cli-parser' => array(
'pretty_version' => '2.0.1',
'version' => '2.0.1.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/cli-parser',
'aliases' => array(),
'reference' => 'c34583b87e7b7a8055bf6c450c2c77ce32a24084',
'dev_requirement' => true,
),
'sebastian/code-unit' => array(
'pretty_version' => '2.0.0',
'version' => '2.0.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/code-unit',
'aliases' => array(),
'reference' => 'a81fee9eef0b7a76af11d121767abc44c104e503',
'dev_requirement' => true,
),
'sebastian/code-unit-reverse-lookup' => array(
'pretty_version' => '3.0.0',
'version' => '3.0.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/code-unit-reverse-lookup',
'aliases' => array(),
'reference' => '5e3a687f7d8ae33fb362c5c0743794bbb2420a1d',
'dev_requirement' => true,
),
'sebastian/comparator' => array(
'pretty_version' => '5.0.1',
'version' => '5.0.1.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/comparator',
'aliases' => array(),
'reference' => '2db5010a484d53ebf536087a70b4a5423c102372',
'dev_requirement' => true,
),
'sebastian/complexity' => array(
'pretty_version' => '3.2.0',
'version' => '3.2.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/complexity',
'aliases' => array(),
'reference' => '68ff824baeae169ec9f2137158ee529584553799',
'dev_requirement' => true,
),
'sebastian/diff' => array(
'pretty_version' => '5.1.1',
'version' => '5.1.1.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/diff',
'aliases' => array(),
'reference' => 'c41e007b4b62af48218231d6c2275e4c9b975b2e',
'dev_requirement' => true,
),
'sebastian/environment' => array(
'pretty_version' => '6.1.0',
'version' => '6.1.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/environment',
'aliases' => array(),
'reference' => '8074dbcd93529b357029f5cc5058fd3e43666984',
'dev_requirement' => true,
),
'sebastian/exporter' => array(
'pretty_version' => '5.1.2',
'version' => '5.1.2.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/exporter',
'aliases' => array(),
'reference' => '955288482d97c19a372d3f31006ab3f37da47adf',
'dev_requirement' => true,
),
'sebastian/global-state' => array(
'pretty_version' => '6.0.2',
'version' => '6.0.2.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/global-state',
'aliases' => array(),
'reference' => '987bafff24ecc4c9ac418cab1145b96dd6e9cbd9',
'dev_requirement' => true,
),
'sebastian/lines-of-code' => array(
'pretty_version' => '2.0.2',
'version' => '2.0.2.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/lines-of-code',
'aliases' => array(),
'reference' => '856e7f6a75a84e339195d48c556f23be2ebf75d0',
'dev_requirement' => true,
),
'sebastian/object-enumerator' => array(
'pretty_version' => '5.0.0',
'version' => '5.0.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/object-enumerator',
'aliases' => array(),
'reference' => '202d0e344a580d7f7d04b3fafce6933e59dae906',
'dev_requirement' => true,
),
'sebastian/object-reflector' => array(
'pretty_version' => '3.0.0',
'version' => '3.0.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/object-reflector',
'aliases' => array(),
'reference' => '24ed13d98130f0e7122df55d06c5c4942a577957',
'dev_requirement' => true,
),
'sebastian/recursion-context' => array(
'pretty_version' => '5.0.0',
'version' => '5.0.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/recursion-context',
'aliases' => array(),
'reference' => '05909fb5bc7df4c52992396d0116aed689f93712',
'dev_requirement' => true,
),
'sebastian/type' => array(
'pretty_version' => '4.0.0',
'version' => '4.0.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/type',
'aliases' => array(),
'reference' => '462699a16464c3944eefc02ebdd77882bd3925bf',
'dev_requirement' => true,
),
'sebastian/version' => array(
'pretty_version' => '4.0.1',
'version' => '4.0.1.0',
'type' => 'library',
'install_path' => __DIR__ . '/../sebastian/version',
'aliases' => array(),
'reference' => 'c51fa83a5d8f43f1402e3f32a005e6262244ef17',
'dev_requirement' => true,
),
'theseer/tokenizer' => array(
'pretty_version' => '1.2.3',
'version' => '1.2.3.0',
'type' => 'library',
'install_path' => __DIR__ . '/../theseer/tokenizer',
'aliases' => array(),
'reference' => '737eda637ed5e28c3413cb1ebe8bb52cbf1ca7a2',
'dev_requirement' => true,
),
),
);

View File

@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2013 My C-Sense
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,406 @@
# DeepCopy
DeepCopy helps you create deep copies (clones) of your objects. It is designed to handle cycles in the association graph.
[![Total Downloads](https://poser.pugx.org/myclabs/deep-copy/downloads.svg)](https://packagist.org/packages/myclabs/deep-copy)
[![Integrate](https://github.com/myclabs/DeepCopy/actions/workflows/ci.yaml/badge.svg?branch=1.x)](https://github.com/myclabs/DeepCopy/actions/workflows/ci.yaml)
## Table of Contents
1. [How](#how)
1. [Why](#why)
1. [Using simply `clone`](#using-simply-clone)
1. [Overriding `__clone()`](#overriding-__clone)
1. [With `DeepCopy`](#with-deepcopy)
1. [How it works](#how-it-works)
1. [Going further](#going-further)
1. [Matchers](#matchers)
1. [Property name](#property-name)
1. [Specific property](#specific-property)
1. [Type](#type)
1. [Filters](#filters)
1. [`SetNullFilter`](#setnullfilter-filter)
1. [`KeepFilter`](#keepfilter-filter)
1. [`DoctrineCollectionFilter`](#doctrinecollectionfilter-filter)
1. [`DoctrineEmptyCollectionFilter`](#doctrineemptycollectionfilter-filter)
1. [`DoctrineProxyFilter`](#doctrineproxyfilter-filter)
1. [`ReplaceFilter`](#replacefilter-type-filter)
1. [`ShallowCopyFilter`](#shallowcopyfilter-type-filter)
1. [Edge cases](#edge-cases)
1. [Contributing](#contributing)
1. [Tests](#tests)
## How?
Install with Composer:
```
composer require myclabs/deep-copy
```
Use it:
```php
use DeepCopy\DeepCopy;
$copier = new DeepCopy();
$myCopy = $copier->copy($myObject);
```
## Why?
- How do you create copies of your objects?
```php
$myCopy = clone $myObject;
```
- How do you create **deep** copies of your objects (i.e. copying also all the objects referenced in the properties)?
You use [`__clone()`](http://www.php.net/manual/en/language.oop5.cloning.php#object.clone) and implement the behavior
yourself.
- But how do you handle **cycles** in the association graph?
Now you're in for a big mess :(
![association graph](doc/graph.png)
### Using simply `clone`
![Using clone](doc/clone.png)
### Overriding `__clone()`
![Overriding __clone](doc/deep-clone.png)
### With `DeepCopy`
![With DeepCopy](doc/deep-copy.png)
## How it works
DeepCopy recursively traverses all the object's properties and clones them. To avoid cloning the same object twice it
keeps a hash map of all instances and thus preserves the object graph.
To use it:
```php
use function DeepCopy\deep_copy;
$copy = deep_copy($var);
```
Alternatively, you can create your own `DeepCopy` instance to configure it differently for example:
```php
use DeepCopy\DeepCopy;
$copier = new DeepCopy(true);
$copy = $copier->copy($var);
```
You may want to roll your own deep copy function:
```php
namespace Acme;
use DeepCopy\DeepCopy;
function deep_copy($var)
{
static $copier = null;
if (null === $copier) {
$copier = new DeepCopy(true);
}
return $copier->copy($var);
}
```
## Going further
You can add filters to customize the copy process.
The method to add a filter is `DeepCopy\DeepCopy::addFilter($filter, $matcher)`,
with `$filter` implementing `DeepCopy\Filter\Filter`
and `$matcher` implementing `DeepCopy\Matcher\Matcher`.
We provide some generic filters and matchers.
### Matchers
- `DeepCopy\Matcher` applies on a object attribute.
- `DeepCopy\TypeMatcher` applies on any element found in graph, including array elements.
#### Property name
The `PropertyNameMatcher` will match a property by its name:
```php
use DeepCopy\Matcher\PropertyNameMatcher;
// Will apply a filter to any property of any objects named "id"
$matcher = new PropertyNameMatcher('id');
```
#### Specific property
The `PropertyMatcher` will match a specific property of a specific class:
```php
use DeepCopy\Matcher\PropertyMatcher;
// Will apply a filter to the property "id" of any objects of the class "MyClass"
$matcher = new PropertyMatcher('MyClass', 'id');
```
#### Type
The `TypeMatcher` will match any element by its type (instance of a class or any value that could be parameter of
[gettype()](http://php.net/manual/en/function.gettype.php) function):
```php
use DeepCopy\TypeMatcher\TypeMatcher;
// Will apply a filter to any object that is an instance of Doctrine\Common\Collections\Collection
$matcher = new TypeMatcher('Doctrine\Common\Collections\Collection');
```
### Filters
- `DeepCopy\Filter` applies a transformation to the object attribute matched by `DeepCopy\Matcher`
- `DeepCopy\TypeFilter` applies a transformation to any element matched by `DeepCopy\TypeMatcher`
By design, matching a filter will stop the chain of filters (i.e. the next ones will not be applied).
Using the ([`ChainableFilter`](#chainablefilter-filter)) won't stop the chain of filters.
#### `SetNullFilter` (filter)
Let's say for example that you are copying a database record (or a Doctrine entity), so you want the copy not to have
any ID:
```php
use DeepCopy\DeepCopy;
use DeepCopy\Filter\SetNullFilter;
use DeepCopy\Matcher\PropertyNameMatcher;
$object = MyClass::load(123);
echo $object->id; // 123
$copier = new DeepCopy();
$copier->addFilter(new SetNullFilter(), new PropertyNameMatcher('id'));
$copy = $copier->copy($object);
echo $copy->id; // null
```
#### `KeepFilter` (filter)
If you want a property to remain untouched (for example, an association to an object):
```php
use DeepCopy\DeepCopy;
use DeepCopy\Filter\KeepFilter;
use DeepCopy\Matcher\PropertyMatcher;
$copier = new DeepCopy();
$copier->addFilter(new KeepFilter(), new PropertyMatcher('MyClass', 'category'));
$copy = $copier->copy($object);
// $copy->category has not been touched
```
#### `ChainableFilter` (filter)
If you use cloning on proxy classes, you might want to apply two filters for:
1. loading the data
2. applying a transformation
You can use the `ChainableFilter` as a decorator of the proxy loader filter, which won't stop the chain of filters (i.e.
the next ones may be applied).
```php
use DeepCopy\DeepCopy;
use DeepCopy\Filter\ChainableFilter;
use DeepCopy\Filter\Doctrine\DoctrineProxyFilter;
use DeepCopy\Filter\SetNullFilter;
use DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher;
use DeepCopy\Matcher\PropertyNameMatcher;
$copier = new DeepCopy();
$copier->addFilter(new ChainableFilter(new DoctrineProxyFilter()), new DoctrineProxyMatcher());
$copier->addFilter(new SetNullFilter(), new PropertyNameMatcher('id'));
$copy = $copier->copy($object);
echo $copy->id; // null
```
#### `DoctrineCollectionFilter` (filter)
If you use Doctrine and want to copy an entity, you will need to use the `DoctrineCollectionFilter`:
```php
use DeepCopy\DeepCopy;
use DeepCopy\Filter\Doctrine\DoctrineCollectionFilter;
use DeepCopy\Matcher\PropertyTypeMatcher;
$copier = new DeepCopy();
$copier->addFilter(new DoctrineCollectionFilter(), new PropertyTypeMatcher('Doctrine\Common\Collections\Collection'));
$copy = $copier->copy($object);
```
#### `DoctrineEmptyCollectionFilter` (filter)
If you use Doctrine and want to copy an entity who contains a `Collection` that you want to be reset, you can use the
`DoctrineEmptyCollectionFilter`
```php
use DeepCopy\DeepCopy;
use DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter;
use DeepCopy\Matcher\PropertyMatcher;
$copier = new DeepCopy();
$copier->addFilter(new DoctrineEmptyCollectionFilter(), new PropertyMatcher('MyClass', 'myProperty'));
$copy = $copier->copy($object);
// $copy->myProperty will return an empty collection
```
#### `DoctrineProxyFilter` (filter)
If you use Doctrine and use cloning on lazy loaded entities, you might encounter errors mentioning missing fields on a
Doctrine proxy class (...\\\_\_CG\_\_\Proxy).
You can use the `DoctrineProxyFilter` to load the actual entity behind the Doctrine proxy class.
**Make sure, though, to put this as one of your very first filters in the filter chain so that the entity is loaded
before other filters are applied!**
We recommend to decorate the `DoctrineProxyFilter` with the `ChainableFilter` to allow applying other filters to the
cloned lazy loaded entities.
```php
use DeepCopy\DeepCopy;
use DeepCopy\Filter\Doctrine\DoctrineProxyFilter;
use DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher;
$copier = new DeepCopy();
$copier->addFilter(new ChainableFilter(new DoctrineProxyFilter()), new DoctrineProxyMatcher());
$copy = $copier->copy($object);
// $copy should now contain a clone of all entities, including those that were not yet fully loaded.
```
#### `ReplaceFilter` (type filter)
1. If you want to replace the value of a property:
```php
use DeepCopy\DeepCopy;
use DeepCopy\Filter\ReplaceFilter;
use DeepCopy\Matcher\PropertyMatcher;
$copier = new DeepCopy();
$callback = function ($currentValue) {
return $currentValue . ' (copy)'
};
$copier->addFilter(new ReplaceFilter($callback), new PropertyMatcher('MyClass', 'title'));
$copy = $copier->copy($object);
// $copy->title will contain the data returned by the callback, e.g. 'The title (copy)'
```
2. If you want to replace whole element:
```php
use DeepCopy\DeepCopy;
use DeepCopy\TypeFilter\ReplaceFilter;
use DeepCopy\TypeMatcher\TypeMatcher;
$copier = new DeepCopy();
$callback = function (MyClass $myClass) {
return get_class($myClass);
};
$copier->addTypeFilter(new ReplaceFilter($callback), new TypeMatcher('MyClass'));
$copy = $copier->copy([new MyClass, 'some string', new MyClass]);
// $copy will contain ['MyClass', 'some string', 'MyClass']
```
The `$callback` parameter of the `ReplaceFilter` constructor accepts any PHP callable.
#### `ShallowCopyFilter` (type filter)
Stop *DeepCopy* from recursively copying element, using standard `clone` instead:
```php
use DeepCopy\DeepCopy;
use DeepCopy\TypeFilter\ShallowCopyFilter;
use DeepCopy\TypeMatcher\TypeMatcher;
use Mockery as m;
$this->deepCopy = new DeepCopy();
$this->deepCopy->addTypeFilter(
new ShallowCopyFilter,
new TypeMatcher(m\MockInterface::class)
);
$myServiceWithMocks = new MyService(m::mock(MyDependency1::class), m::mock(MyDependency2::class));
// All mocks will be just cloned, not deep copied
```
## Edge cases
The following structures cannot be deep-copied with PHP Reflection. As a result they are shallow cloned and filters are
not applied. There is two ways for you to handle them:
- Implement your own `__clone()` method
- Use a filter with a type matcher
## Contributing
DeepCopy is distributed under the MIT license.
### Tests
Running the tests is simple:
```php
vendor/bin/phpunit
```
### Support
Get professional support via [the Tidelift Subscription](https://tidelift.com/subscription/pkg/packagist-myclabs-deep-copy?utm_source=packagist-myclabs-deep-copy&utm_medium=referral&utm_campaign=readme).

View File

@ -0,0 +1,43 @@
{
"name": "myclabs/deep-copy",
"description": "Create deep copies (clones) of your objects",
"license": "MIT",
"type": "library",
"keywords": [
"clone",
"copy",
"duplicate",
"object",
"object graph"
],
"require": {
"php": "^7.1 || ^8.0"
},
"require-dev": {
"doctrine/collections": "^1.6.8",
"doctrine/common": "^2.13.3 || ^3.2.2",
"phpspec/prophecy": "^1.10",
"phpunit/phpunit": "^7.5.20 || ^8.5.23 || ^9.5.13"
},
"conflict": {
"doctrine/collections": "<1.6.8",
"doctrine/common": "<2.13.3 || >=3 <3.2.2"
},
"autoload": {
"psr-4": {
"DeepCopy\\": "src/DeepCopy/"
},
"files": [
"src/DeepCopy/deep_copy.php"
]
},
"autoload-dev": {
"psr-4": {
"DeepCopyTest\\": "tests/DeepCopyTest/",
"DeepCopy\\": "fixtures/"
}
},
"config": {
"sort-packages": true
}
}

View File

@ -0,0 +1,313 @@
<?php
namespace DeepCopy;
use ArrayObject;
use DateInterval;
use DateTimeInterface;
use DateTimeZone;
use DeepCopy\Exception\CloneException;
use DeepCopy\Filter\ChainableFilter;
use DeepCopy\Filter\Filter;
use DeepCopy\Matcher\Matcher;
use DeepCopy\Reflection\ReflectionHelper;
use DeepCopy\TypeFilter\Date\DateIntervalFilter;
use DeepCopy\TypeFilter\Spl\ArrayObjectFilter;
use DeepCopy\TypeFilter\Spl\SplDoublyLinkedListFilter;
use DeepCopy\TypeFilter\TypeFilter;
use DeepCopy\TypeMatcher\TypeMatcher;
use ReflectionObject;
use ReflectionProperty;
use SplDoublyLinkedList;
/**
* @final
*/
class DeepCopy
{
/**
* @var object[] List of objects copied.
*/
private $hashMap = [];
/**
* Filters to apply.
*
* @var array Array of ['filter' => Filter, 'matcher' => Matcher] pairs.
*/
private $filters = [];
/**
* Type Filters to apply.
*
* @var array Array of ['filter' => Filter, 'matcher' => Matcher] pairs.
*/
private $typeFilters = [];
/**
* @var bool
*/
private $skipUncloneable = false;
/**
* @var bool
*/
private $useCloneMethod;
/**
* @param bool $useCloneMethod If set to true, when an object implements the __clone() function, it will be used
* instead of the regular deep cloning.
*/
public function __construct($useCloneMethod = false)
{
$this->useCloneMethod = $useCloneMethod;
$this->addTypeFilter(new ArrayObjectFilter($this), new TypeMatcher(ArrayObject::class));
$this->addTypeFilter(new DateIntervalFilter(), new TypeMatcher(DateInterval::class));
$this->addTypeFilter(new SplDoublyLinkedListFilter($this), new TypeMatcher(SplDoublyLinkedList::class));
}
/**
* If enabled, will not throw an exception when coming across an uncloneable property.
*
* @param $skipUncloneable
*
* @return $this
*/
public function skipUncloneable($skipUncloneable = true)
{
$this->skipUncloneable = $skipUncloneable;
return $this;
}
/**
* Deep copies the given object.
*
* @param mixed $object
*
* @return mixed
*/
public function copy($object)
{
$this->hashMap = [];
return $this->recursiveCopy($object);
}
public function addFilter(Filter $filter, Matcher $matcher)
{
$this->filters[] = [
'matcher' => $matcher,
'filter' => $filter,
];
}
public function prependFilter(Filter $filter, Matcher $matcher)
{
array_unshift($this->filters, [
'matcher' => $matcher,
'filter' => $filter,
]);
}
public function addTypeFilter(TypeFilter $filter, TypeMatcher $matcher)
{
$this->typeFilters[] = [
'matcher' => $matcher,
'filter' => $filter,
];
}
private function recursiveCopy($var)
{
// Matches Type Filter
if ($filter = $this->getFirstMatchedTypeFilter($this->typeFilters, $var)) {
return $filter->apply($var);
}
// Resource
if (is_resource($var)) {
return $var;
}
// Array
if (is_array($var)) {
return $this->copyArray($var);
}
// Scalar
if (! is_object($var)) {
return $var;
}
// Enum
if (PHP_VERSION_ID >= 80100 && enum_exists(get_class($var))) {
return $var;
}
// Object
return $this->copyObject($var);
}
/**
* Copy an array
* @param array $array
* @return array
*/
private function copyArray(array $array)
{
foreach ($array as $key => $value) {
$array[$key] = $this->recursiveCopy($value);
}
return $array;
}
/**
* Copies an object.
*
* @param object $object
*
* @throws CloneException
*
* @return object
*/
private function copyObject($object)
{
$objectHash = spl_object_hash($object);
if (isset($this->hashMap[$objectHash])) {
return $this->hashMap[$objectHash];
}
$reflectedObject = new ReflectionObject($object);
$isCloneable = $reflectedObject->isCloneable();
if (false === $isCloneable) {
if ($this->skipUncloneable) {
$this->hashMap[$objectHash] = $object;
return $object;
}
throw new CloneException(
sprintf(
'The class "%s" is not cloneable.',
$reflectedObject->getName()
)
);
}
$newObject = clone $object;
$this->hashMap[$objectHash] = $newObject;
if ($this->useCloneMethod && $reflectedObject->hasMethod('__clone')) {
return $newObject;
}
if ($newObject instanceof DateTimeInterface || $newObject instanceof DateTimeZone) {
return $newObject;
}
foreach (ReflectionHelper::getProperties($reflectedObject) as $property) {
$this->copyObjectProperty($newObject, $property);
}
return $newObject;
}
private function copyObjectProperty($object, ReflectionProperty $property)
{
// Ignore static properties
if ($property->isStatic()) {
return;
}
// Ignore readonly properties
if (method_exists($property, 'isReadOnly') && $property->isReadOnly()) {
return;
}
// Apply the filters
foreach ($this->filters as $item) {
/** @var Matcher $matcher */
$matcher = $item['matcher'];
/** @var Filter $filter */
$filter = $item['filter'];
if ($matcher->matches($object, $property->getName())) {
$filter->apply(
$object,
$property->getName(),
function ($object) {
return $this->recursiveCopy($object);
}
);
if ($filter instanceof ChainableFilter) {
continue;
}
// If a filter matches, we stop processing this property
return;
}
}
$property->setAccessible(true);
// Ignore uninitialized properties (for PHP >7.4)
if (method_exists($property, 'isInitialized') && !$property->isInitialized($object)) {
return;
}
$propertyValue = $property->getValue($object);
// Copy the property
$property->setValue($object, $this->recursiveCopy($propertyValue));
}
/**
* Returns first filter that matches variable, `null` if no such filter found.
*
* @param array $filterRecords Associative array with 2 members: 'filter' with value of type {@see TypeFilter} and
* 'matcher' with value of type {@see TypeMatcher}
* @param mixed $var
*
* @return TypeFilter|null
*/
private function getFirstMatchedTypeFilter(array $filterRecords, $var)
{
$matched = $this->first(
$filterRecords,
function (array $record) use ($var) {
/* @var TypeMatcher $matcher */
$matcher = $record['matcher'];
return $matcher->matches($var);
}
);
return isset($matched) ? $matched['filter'] : null;
}
/**
* Returns first element that matches predicate, `null` if no such element found.
*
* @param array $elements Array of ['filter' => Filter, 'matcher' => Matcher] pairs.
* @param callable $predicate Predicate arguments are: element.
*
* @return array|null Associative array with 2 members: 'filter' with value of type {@see TypeFilter} and 'matcher'
* with value of type {@see TypeMatcher} or `null`.
*/
private function first(array $elements, callable $predicate)
{
foreach ($elements as $element) {
if (call_user_func($predicate, $element)) {
return $element;
}
}
return null;
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace DeepCopy\Exception;
use UnexpectedValueException;
class CloneException extends UnexpectedValueException
{
}

View File

@ -0,0 +1,9 @@
<?php
namespace DeepCopy\Exception;
use ReflectionException;
class PropertyException extends ReflectionException
{
}

View File

@ -0,0 +1,24 @@
<?php
namespace DeepCopy\Filter;
/**
* Defines a decorator filter that will not stop the chain of filters.
*/
class ChainableFilter implements Filter
{
/**
* @var Filter
*/
protected $filter;
public function __construct(Filter $filter)
{
$this->filter = $filter;
}
public function apply($object, $property, $objectCopier)
{
$this->filter->apply($object, $property, $objectCopier);
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace DeepCopy\Filter\Doctrine;
use DeepCopy\Filter\Filter;
use DeepCopy\Reflection\ReflectionHelper;
/**
* @final
*/
class DoctrineCollectionFilter implements Filter
{
/**
* Copies the object property doctrine collection.
*
* {@inheritdoc}
*/
public function apply($object, $property, $objectCopier)
{
$reflectionProperty = ReflectionHelper::getProperty($object, $property);
$reflectionProperty->setAccessible(true);
$oldCollection = $reflectionProperty->getValue($object);
$newCollection = $oldCollection->map(
function ($item) use ($objectCopier) {
return $objectCopier($item);
}
);
$reflectionProperty->setValue($object, $newCollection);
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace DeepCopy\Filter\Doctrine;
use DeepCopy\Filter\Filter;
use DeepCopy\Reflection\ReflectionHelper;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @final
*/
class DoctrineEmptyCollectionFilter implements Filter
{
/**
* Sets the object property to an empty doctrine collection.
*
* @param object $object
* @param string $property
* @param callable $objectCopier
*/
public function apply($object, $property, $objectCopier)
{
$reflectionProperty = ReflectionHelper::getProperty($object, $property);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($object, new ArrayCollection());
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace DeepCopy\Filter\Doctrine;
use DeepCopy\Filter\Filter;
/**
* @final
*/
class DoctrineProxyFilter implements Filter
{
/**
* Triggers the magic method __load() on a Doctrine Proxy class to load the
* actual entity from the database.
*
* {@inheritdoc}
*/
public function apply($object, $property, $objectCopier)
{
$object->__load();
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace DeepCopy\Filter;
/**
* Filter to apply to a property while copying an object
*/
interface Filter
{
/**
* Applies the filter to the object.
*
* @param object $object
* @param string $property
* @param callable $objectCopier
*/
public function apply($object, $property, $objectCopier);
}

View File

@ -0,0 +1,16 @@
<?php
namespace DeepCopy\Filter;
class KeepFilter implements Filter
{
/**
* Keeps the value of the object property.
*
* {@inheritdoc}
*/
public function apply($object, $property, $objectCopier)
{
// Nothing to do
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace DeepCopy\Filter;
use DeepCopy\Reflection\ReflectionHelper;
/**
* @final
*/
class ReplaceFilter implements Filter
{
/**
* @var callable
*/
protected $callback;
/**
* @param callable $callable Will be called to get the new value for each property to replace
*/
public function __construct(callable $callable)
{
$this->callback = $callable;
}
/**
* Replaces the object property by the result of the callback called with the object property.
*
* {@inheritdoc}
*/
public function apply($object, $property, $objectCopier)
{
$reflectionProperty = ReflectionHelper::getProperty($object, $property);
$reflectionProperty->setAccessible(true);
$value = call_user_func($this->callback, $reflectionProperty->getValue($object));
$reflectionProperty->setValue($object, $value);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace DeepCopy\Filter;
use DeepCopy\Reflection\ReflectionHelper;
/**
* @final
*/
class SetNullFilter implements Filter
{
/**
* Sets the object property to null.
*
* {@inheritdoc}
*/
public function apply($object, $property, $objectCopier)
{
$reflectionProperty = ReflectionHelper::getProperty($object, $property);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($object, null);
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace DeepCopy\Matcher\Doctrine;
use DeepCopy\Matcher\Matcher;
use Doctrine\Persistence\Proxy;
/**
* @final
*/
class DoctrineProxyMatcher implements Matcher
{
/**
* Matches a Doctrine Proxy class.
*
* {@inheritdoc}
*/
public function matches($object, $property)
{
return $object instanceof Proxy;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace DeepCopy\Matcher;
interface Matcher
{
/**
* @param object $object
* @param string $property
*
* @return boolean
*/
public function matches($object, $property);
}

View File

@ -0,0 +1,39 @@
<?php
namespace DeepCopy\Matcher;
/**
* @final
*/
class PropertyMatcher implements Matcher
{
/**
* @var string
*/
private $class;
/**
* @var string
*/
private $property;
/**
* @param string $class Class name
* @param string $property Property name
*/
public function __construct($class, $property)
{
$this->class = $class;
$this->property = $property;
}
/**
* Matches a specific property of a specific class.
*
* {@inheritdoc}
*/
public function matches($object, $property)
{
return ($object instanceof $this->class) && $property == $this->property;
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace DeepCopy\Matcher;
/**
* @final
*/
class PropertyNameMatcher implements Matcher
{
/**
* @var string
*/
private $property;
/**
* @param string $property Property name
*/
public function __construct($property)
{
$this->property = $property;
}
/**
* Matches a property by its name.
*
* {@inheritdoc}
*/
public function matches($object, $property)
{
return $property == $this->property;
}
}

View File

@ -0,0 +1,52 @@
<?php
namespace DeepCopy\Matcher;
use DeepCopy\Reflection\ReflectionHelper;
use ReflectionException;
/**
* Matches a property by its type.
*
* It is recommended to use {@see DeepCopy\TypeFilter\TypeFilter} instead, as it applies on all occurrences
* of given type in copied context (eg. array elements), not just on object properties.
*
* @final
*/
class PropertyTypeMatcher implements Matcher
{
/**
* @var string
*/
private $propertyType;
/**
* @param string $propertyType Property type
*/
public function __construct($propertyType)
{
$this->propertyType = $propertyType;
}
/**
* {@inheritdoc}
*/
public function matches($object, $property)
{
try {
$reflectionProperty = ReflectionHelper::getProperty($object, $property);
} catch (ReflectionException $exception) {
return false;
}
$reflectionProperty->setAccessible(true);
// Uninitialized properties (for PHP >7.4)
if (method_exists($reflectionProperty, 'isInitialized') && !$reflectionProperty->isInitialized($object)) {
// null instanceof $this->propertyType
return false;
}
return $reflectionProperty->getValue($object) instanceof $this->propertyType;
}
}

View File

@ -0,0 +1,78 @@
<?php
namespace DeepCopy\Reflection;
use DeepCopy\Exception\PropertyException;
use ReflectionClass;
use ReflectionException;
use ReflectionObject;
use ReflectionProperty;
class ReflectionHelper
{
/**
* Retrieves all properties (including private ones), from object and all its ancestors.
*
* Standard \ReflectionClass->getProperties() does not return private properties from ancestor classes.
*
* @author muratyaman@gmail.com
* @see http://php.net/manual/en/reflectionclass.getproperties.php
*
* @param ReflectionClass $ref
*
* @return ReflectionProperty[]
*/
public static function getProperties(ReflectionClass $ref)
{
$props = $ref->getProperties();
$propsArr = array();
foreach ($props as $prop) {
$propertyName = $prop->getName();
$propsArr[$propertyName] = $prop;
}
if ($parentClass = $ref->getParentClass()) {
$parentPropsArr = self::getProperties($parentClass);
foreach ($propsArr as $key => $property) {
$parentPropsArr[$key] = $property;
}
return $parentPropsArr;
}
return $propsArr;
}
/**
* Retrieves property by name from object and all its ancestors.
*
* @param object|string $object
* @param string $name
*
* @throws PropertyException
* @throws ReflectionException
*
* @return ReflectionProperty
*/
public static function getProperty($object, $name)
{
$reflection = is_object($object) ? new ReflectionObject($object) : new ReflectionClass($object);
if ($reflection->hasProperty($name)) {
return $reflection->getProperty($name);
}
if ($parentClass = $reflection->getParentClass()) {
return self::getProperty($parentClass->getName(), $name);
}
throw new PropertyException(
sprintf(
'The class "%s" doesn\'t have a property with the given name: "%s".',
is_object($object) ? get_class($object) : $object,
$name
)
);
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace DeepCopy\TypeFilter\Date;
use DateInterval;
use DeepCopy\TypeFilter\TypeFilter;
/**
* @final
*
* @deprecated Will be removed in 2.0. This filter will no longer be necessary in PHP 7.1+.
*/
class DateIntervalFilter implements TypeFilter
{
/**
* {@inheritdoc}
*
* @param DateInterval $element
*
* @see http://news.php.net/php.bugs/205076
*/
public function apply($element)
{
$copy = new DateInterval('P0D');
foreach ($element as $propertyName => $propertyValue) {
$copy->{$propertyName} = $propertyValue;
}
return $copy;
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace DeepCopy\TypeFilter;
/**
* @final
*/
class ReplaceFilter implements TypeFilter
{
/**
* @var callable
*/
protected $callback;
/**
* @param callable $callable Will be called to get the new value for each element to replace
*/
public function __construct(callable $callable)
{
$this->callback = $callable;
}
/**
* {@inheritdoc}
*/
public function apply($element)
{
return call_user_func($this->callback, $element);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace DeepCopy\TypeFilter;
/**
* @final
*/
class ShallowCopyFilter implements TypeFilter
{
/**
* {@inheritdoc}
*/
public function apply($element)
{
return clone $element;
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace DeepCopy\TypeFilter\Spl;
use DeepCopy\DeepCopy;
use DeepCopy\TypeFilter\TypeFilter;
/**
* In PHP 7.4 the storage of an ArrayObject isn't returned as
* ReflectionProperty. So we deep copy its array copy.
*/
final class ArrayObjectFilter implements TypeFilter
{
/**
* @var DeepCopy
*/
private $copier;
public function __construct(DeepCopy $copier)
{
$this->copier = $copier;
}
/**
* {@inheritdoc}
*/
public function apply($arrayObject)
{
$clone = clone $arrayObject;
foreach ($arrayObject->getArrayCopy() as $k => $v) {
$clone->offsetSet($k, $this->copier->copy($v));
}
return $clone;
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace DeepCopy\TypeFilter\Spl;
/**
* @deprecated Use {@see SplDoublyLinkedListFilter} instead.
*/
class SplDoublyLinkedList extends SplDoublyLinkedListFilter
{
}

View File

@ -0,0 +1,51 @@
<?php
namespace DeepCopy\TypeFilter\Spl;
use Closure;
use DeepCopy\DeepCopy;
use DeepCopy\TypeFilter\TypeFilter;
use SplDoublyLinkedList;
/**
* @final
*/
class SplDoublyLinkedListFilter implements TypeFilter
{
private $copier;
public function __construct(DeepCopy $copier)
{
$this->copier = $copier;
}
/**
* {@inheritdoc}
*/
public function apply($element)
{
$newElement = clone $element;
$copy = $this->createCopyClosure();
return $copy($newElement);
}
private function createCopyClosure()
{
$copier = $this->copier;
$copy = function (SplDoublyLinkedList $list) use ($copier) {
// Replace each element in the list with a deep copy of itself
for ($i = 1; $i <= $list->count(); $i++) {
$copy = $copier->recursiveCopy($list->shift());
$list->push($copy);
}
return $list;
};
return Closure::bind($copy, null, DeepCopy::class);
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace DeepCopy\TypeFilter;
interface TypeFilter
{
/**
* Applies the filter to the object.
*
* @param mixed $element
*/
public function apply($element);
}

View File

@ -0,0 +1,29 @@
<?php
namespace DeepCopy\TypeMatcher;
class TypeMatcher
{
/**
* @var string
*/
private $type;
/**
* @param string $type
*/
public function __construct($type)
{
$this->type = $type;
}
/**
* @param mixed $element
*
* @return boolean
*/
public function matches($element)
{
return is_object($element) ? is_a($element, $this->type) : gettype($element) === $this->type;
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace DeepCopy;
use function function_exists;
if (false === function_exists('DeepCopy\deep_copy')) {
/**
* Deep copies the given value.
*
* @param mixed $value
* @param bool $useCloneMethod
*
* @return mixed
*/
function deep_copy($value, $useCloneMethod = false)
{
return (new DeepCopy($useCloneMethod))->copy($value);
}
}

View File

@ -0,0 +1,29 @@
BSD 3-Clause License
Copyright (c) 2011, Nikita Popov
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -0,0 +1,233 @@
PHP Parser
==========
[![Coverage Status](https://coveralls.io/repos/github/nikic/PHP-Parser/badge.svg?branch=master)](https://coveralls.io/github/nikic/PHP-Parser?branch=master)
This is a PHP parser written in PHP. Its purpose is to simplify static code analysis and
manipulation.
[**Documentation for version 5.x**][doc_master] (current; for running on PHP >= 7.4; for parsing PHP 7.0 to PHP 8.3, with limited support for parsing PHP 5.x).
[Documentation for version 4.x][doc_4_x] (supported; for running on PHP >= 7.0; for parsing PHP 5.2 to PHP 8.3).
Features
--------
The main features provided by this library are:
* Parsing PHP 7, and PHP 8 code into an abstract syntax tree (AST).
* Invalid code can be parsed into a partial AST.
* The AST contains accurate location information.
* Dumping the AST in human-readable form.
* Converting an AST back to PHP code.
* Formatting can be preserved for partially changed ASTs.
* Infrastructure to traverse and modify ASTs.
* Resolution of namespaced names.
* Evaluation of constant expressions.
* Builders to simplify AST construction for code generation.
* Converting an AST into JSON and back.
Quick Start
-----------
Install the library using [composer](https://getcomposer.org):
php composer.phar require nikic/php-parser
Parse some PHP code into an AST and dump the result in human-readable form:
```php
<?php
use PhpParser\Error;
use PhpParser\NodeDumper;
use PhpParser\ParserFactory;
$code = <<<'CODE'
<?php
function test($foo)
{
var_dump($foo);
}
CODE;
$parser = (new ParserFactory())->createForNewestSupportedVersion();
try {
$ast = $parser->parse($code);
} catch (Error $error) {
echo "Parse error: {$error->getMessage()}\n";
return;
}
$dumper = new NodeDumper;
echo $dumper->dump($ast) . "\n";
```
This dumps an AST looking something like this:
```
array(
0: Stmt_Function(
attrGroups: array(
)
byRef: false
name: Identifier(
name: test
)
params: array(
0: Param(
attrGroups: array(
)
flags: 0
type: null
byRef: false
variadic: false
var: Expr_Variable(
name: foo
)
default: null
)
)
returnType: null
stmts: array(
0: Stmt_Expression(
expr: Expr_FuncCall(
name: Name(
name: var_dump
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: foo
)
byRef: false
unpack: false
)
)
)
)
)
)
)
```
Let's traverse the AST and perform some kind of modification. For example, drop all function bodies:
```php
use PhpParser\Node;
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
$traverser = new NodeTraverser();
$traverser->addVisitor(new class extends NodeVisitorAbstract {
public function enterNode(Node $node) {
if ($node instanceof Function_) {
// Clean out the function body
$node->stmts = [];
}
}
});
$ast = $traverser->traverse($ast);
echo $dumper->dump($ast) . "\n";
```
This gives us an AST where the `Function_::$stmts` are empty:
```
array(
0: Stmt_Function(
attrGroups: array(
)
byRef: false
name: Identifier(
name: test
)
params: array(
0: Param(
attrGroups: array(
)
type: null
byRef: false
variadic: false
var: Expr_Variable(
name: foo
)
default: null
)
)
returnType: null
stmts: array(
)
)
)
```
Finally, we can convert the new AST back to PHP code:
```php
use PhpParser\PrettyPrinter;
$prettyPrinter = new PrettyPrinter\Standard;
echo $prettyPrinter->prettyPrintFile($ast);
```
This gives us our original code, minus the `var_dump()` call inside the function:
```php
<?php
function test($foo)
{
}
```
For a more comprehensive introduction, see the documentation.
Documentation
-------------
1. [Introduction](doc/0_Introduction.markdown)
2. [Usage of basic components](doc/2_Usage_of_basic_components.markdown)
Component documentation:
* [Walking the AST](doc/component/Walking_the_AST.markdown)
* Node visitors
* Modifying the AST from a visitor
* Short-circuiting traversals
* Interleaved visitors
* Simple node finding API
* Parent and sibling references
* [Name resolution](doc/component/Name_resolution.markdown)
* Name resolver options
* Name resolution context
* [Pretty printing](doc/component/Pretty_printing.markdown)
* Converting AST back to PHP code
* Customizing formatting
* Formatting-preserving code transformations
* [AST builders](doc/component/AST_builders.markdown)
* Fluent builders for AST nodes
* [Lexer](doc/component/Lexer.markdown)
* Emulation
* Tokens, positions and attributes
* [Error handling](doc/component/Error_handling.markdown)
* Column information for errors
* Error recovery (parsing of syntactically incorrect code)
* [Constant expression evaluation](doc/component/Constant_expression_evaluation.markdown)
* Evaluating constant/property/etc initializers
* Handling errors and unsupported expressions
* [JSON representation](doc/component/JSON_representation.markdown)
* JSON encoding and decoding of ASTs
* [Performance](doc/component/Performance.markdown)
* Disabling Xdebug
* Reusing objects
* Garbage collection impact
* [Frequently asked questions](doc/component/FAQ.markdown)
* Parent and sibling references
[doc_3_x]: https://github.com/nikic/PHP-Parser/tree/3.x/doc
[doc_4_x]: https://github.com/nikic/PHP-Parser/tree/4.x/doc
[doc_master]: https://github.com/nikic/PHP-Parser/tree/master/doc

View File

@ -0,0 +1,206 @@
#!/usr/bin/env php
<?php
foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
if (file_exists($file)) {
require $file;
break;
}
}
ini_set('xdebug.max_nesting_level', 3000);
// Disable Xdebug var_dump() output truncation
ini_set('xdebug.var_display_max_children', -1);
ini_set('xdebug.var_display_max_data', -1);
ini_set('xdebug.var_display_max_depth', -1);
list($operations, $files, $attributes) = parseArgs($argv);
/* Dump nodes by default */
if (empty($operations)) {
$operations[] = 'dump';
}
if (empty($files)) {
showHelp("Must specify at least one file.");
}
$parser = (new PhpParser\ParserFactory())->createForVersion($attributes['version']);
$dumper = new PhpParser\NodeDumper([
'dumpComments' => true,
'dumpPositions' => $attributes['with-positions'],
]);
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
$traverser = new PhpParser\NodeTraverser();
$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
foreach ($files as $file) {
if ($file === '-') {
$code = file_get_contents('php://stdin');
fwrite(STDERR, "====> Stdin:\n");
} else if (strpos($file, '<?php') === 0) {
$code = $file;
fwrite(STDERR, "====> Code $code\n");
} else {
if (!file_exists($file)) {
fwrite(STDERR, "File $file does not exist.\n");
exit(1);
}
$code = file_get_contents($file);
fwrite(STDERR, "====> File $file:\n");
}
if ($attributes['with-recovery']) {
$errorHandler = new PhpParser\ErrorHandler\Collecting;
$stmts = $parser->parse($code, $errorHandler);
foreach ($errorHandler->getErrors() as $error) {
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
fwrite(STDERR, $message . "\n");
}
if (null === $stmts) {
continue;
}
} else {
try {
$stmts = $parser->parse($code);
} catch (PhpParser\Error $error) {
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
fwrite(STDERR, $message . "\n");
exit(1);
}
}
foreach ($operations as $operation) {
if ('dump' === $operation) {
fwrite(STDERR, "==> Node dump:\n");
echo $dumper->dump($stmts, $code), "\n";
} elseif ('pretty-print' === $operation) {
fwrite(STDERR, "==> Pretty print:\n");
echo $prettyPrinter->prettyPrintFile($stmts), "\n";
} elseif ('json-dump' === $operation) {
fwrite(STDERR, "==> JSON dump:\n");
echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
} elseif ('var-dump' === $operation) {
fwrite(STDERR, "==> var_dump():\n");
var_dump($stmts);
} elseif ('resolve-names' === $operation) {
fwrite(STDERR, "==> Resolved names.\n");
$stmts = $traverser->traverse($stmts);
}
}
}
function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
if ($withColumnInfo && $e->hasColumnInfo()) {
return $e->getMessageWithColumnInfo($code);
} else {
return $e->getMessage();
}
}
function showHelp($error = '') {
if ($error) {
fwrite(STDERR, $error . "\n\n");
}
fwrite($error ? STDERR : STDOUT, <<<'OUTPUT'
Usage: php-parse [operations] file1.php [file2.php ...]
or: php-parse [operations] "<?php code"
Turn PHP source code into an abstract syntax tree.
Operations is a list of the following options (--dump by default):
-d, --dump Dump nodes using NodeDumper
-p, --pretty-print Pretty print file using PrettyPrinter\Standard
-j, --json-dump Print json_encode() result
--var-dump var_dump() nodes (for exact structure)
-N, --resolve-names Resolve names using NodeVisitor\NameResolver
-c, --with-column-info Show column-numbers for errors (if available)
-P, --with-positions Show positions in node dumps
-r, --with-recovery Use parsing with error recovery
--version=VERSION Target specific PHP version (default: newest)
-h, --help Display this page
Example:
php-parse -d -p -N -d file.php
Dumps nodes, pretty prints them, then resolves names and dumps them again.
OUTPUT
);
exit($error ? 1 : 0);
}
function parseArgs($args) {
$operations = [];
$files = [];
$attributes = [
'with-column-info' => false,
'with-positions' => false,
'with-recovery' => false,
'version' => PhpParser\PhpVersion::getNewestSupported(),
];
array_shift($args);
$parseOptions = true;
foreach ($args as $arg) {
if (!$parseOptions) {
$files[] = $arg;
continue;
}
switch ($arg) {
case '--dump':
case '-d':
$operations[] = 'dump';
break;
case '--pretty-print':
case '-p':
$operations[] = 'pretty-print';
break;
case '--json-dump':
case '-j':
$operations[] = 'json-dump';
break;
case '--var-dump':
$operations[] = 'var-dump';
break;
case '--resolve-names':
case '-N';
$operations[] = 'resolve-names';
break;
case '--with-column-info':
case '-c';
$attributes['with-column-info'] = true;
break;
case '--with-positions':
case '-P':
$attributes['with-positions'] = true;
break;
case '--with-recovery':
case '-r':
$attributes['with-recovery'] = true;
break;
case '--help':
case '-h';
showHelp();
break;
case '--':
$parseOptions = false;
break;
default:
if (preg_match('/^--version=(.*)$/', $arg, $matches)) {
$attributes['version'] = PhpParser\PhpVersion::fromString($matches[1]);
} elseif ($arg[0] === '-' && \strlen($arg[0]) > 1) {
showHelp("Invalid operation $arg.");
} else {
$files[] = $arg;
}
}
}
return [$operations, $files, $attributes];
}

View File

@ -0,0 +1,43 @@
{
"name": "nikic/php-parser",
"type": "library",
"description": "A PHP parser written in PHP",
"keywords": [
"php",
"parser"
],
"license": "BSD-3-Clause",
"authors": [
{
"name": "Nikita Popov"
}
],
"require": {
"php": ">=7.4",
"ext-tokenizer": "*",
"ext-json": "*",
"ext-ctype": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0",
"ircmaxell/php-yacc": "^0.0.7"
},
"extra": {
"branch-alias": {
"dev-master": "5.0-dev"
}
},
"autoload": {
"psr-4": {
"PhpParser\\": "lib/PhpParser"
}
},
"autoload-dev": {
"psr-4": {
"PhpParser\\": "test/PhpParser/"
}
},
"bin": [
"bin/php-parse"
]
}

View File

@ -0,0 +1,12 @@
<?php declare(strict_types=1);
namespace PhpParser;
interface Builder {
/**
* Returns the built node.
*
* @return Node The built node
*/
public function getNode(): Node;
}

View File

@ -0,0 +1,150 @@
<?php
declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\BuilderHelpers;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Const_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
class ClassConst implements PhpParser\Builder {
protected int $flags = 0;
/** @var array<string, mixed> */
protected array $attributes = [];
/** @var list<Const_> */
protected array $constants = [];
/** @var list<Node\AttributeGroup> */
protected array $attributeGroups = [];
/** @var Identifier|Node\Name|Node\ComplexType|null */
protected ?Node $type = null;
/**
* Creates a class constant builder
*
* @param string|Identifier $name Name
* @param Node\Expr|bool|null|int|float|string|array $value Value
*/
public function __construct($name, $value) {
$this->constants = [new Const_($name, BuilderHelpers::normalizeValue($value))];
}
/**
* Add another constant to const group
*
* @param string|Identifier $name Name
* @param Node\Expr|bool|null|int|float|string|array $value Value
*
* @return $this The builder instance (for fluid interface)
*/
public function addConst($name, $value) {
$this->constants[] = new Const_($name, BuilderHelpers::normalizeValue($value));
return $this;
}
/**
* Makes the constant public.
*
* @return $this The builder instance (for fluid interface)
*/
public function makePublic() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC);
return $this;
}
/**
* Makes the constant protected.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeProtected() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED);
return $this;
}
/**
* Makes the constant private.
*
* @return $this The builder instance (for fluid interface)
*/
public function makePrivate() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE);
return $this;
}
/**
* Makes the constant final.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeFinal() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::FINAL);
return $this;
}
/**
* Sets doc comment for the constant.
*
* @param PhpParser\Comment\Doc|string $docComment Doc comment to set
*
* @return $this The builder instance (for fluid interface)
*/
public function setDocComment($docComment) {
$this->attributes = [
'comments' => [BuilderHelpers::normalizeDocComment($docComment)]
];
return $this;
}
/**
* Adds an attribute group.
*
* @param Node\Attribute|Node\AttributeGroup $attribute
*
* @return $this The builder instance (for fluid interface)
*/
public function addAttribute($attribute) {
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
return $this;
}
/**
* Sets the constant type.
*
* @param string|Node\Name|Identifier|Node\ComplexType $type
*
* @return $this
*/
public function setType($type) {
$this->type = BuilderHelpers::normalizeType($type);
return $this;
}
/**
* Returns the built class node.
*
* @return Stmt\ClassConst The built constant node
*/
public function getNode(): PhpParser\Node {
return new Stmt\ClassConst(
$this->constants,
$this->flags,
$this->attributes,
$this->attributeGroups,
$this->type
);
}
}

View File

@ -0,0 +1,151 @@
<?php declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\BuilderHelpers;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
class Class_ extends Declaration {
protected string $name;
protected ?Name $extends = null;
/** @var list<Name> */
protected array $implements = [];
protected int $flags = 0;
/** @var list<Stmt\TraitUse> */
protected array $uses = [];
/** @var list<Stmt\ClassConst> */
protected array $constants = [];
/** @var list<Stmt\Property> */
protected array $properties = [];
/** @var list<Stmt\ClassMethod> */
protected array $methods = [];
/** @var list<Node\AttributeGroup> */
protected array $attributeGroups = [];
/**
* Creates a class builder.
*
* @param string $name Name of the class
*/
public function __construct(string $name) {
$this->name = $name;
}
/**
* Extends a class.
*
* @param Name|string $class Name of class to extend
*
* @return $this The builder instance (for fluid interface)
*/
public function extend($class) {
$this->extends = BuilderHelpers::normalizeName($class);
return $this;
}
/**
* Implements one or more interfaces.
*
* @param Name|string ...$interfaces Names of interfaces to implement
*
* @return $this The builder instance (for fluid interface)
*/
public function implement(...$interfaces) {
foreach ($interfaces as $interface) {
$this->implements[] = BuilderHelpers::normalizeName($interface);
}
return $this;
}
/**
* Makes the class abstract.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeAbstract() {
$this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::ABSTRACT);
return $this;
}
/**
* Makes the class final.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeFinal() {
$this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::FINAL);
return $this;
}
/**
* Makes the class readonly.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeReadonly() {
$this->flags = BuilderHelpers::addClassModifier($this->flags, Modifiers::READONLY);
return $this;
}
/**
* Adds a statement.
*
* @param Stmt|PhpParser\Builder $stmt The statement to add
*
* @return $this The builder instance (for fluid interface)
*/
public function addStmt($stmt) {
$stmt = BuilderHelpers::normalizeNode($stmt);
if ($stmt instanceof Stmt\Property) {
$this->properties[] = $stmt;
} elseif ($stmt instanceof Stmt\ClassMethod) {
$this->methods[] = $stmt;
} elseif ($stmt instanceof Stmt\TraitUse) {
$this->uses[] = $stmt;
} elseif ($stmt instanceof Stmt\ClassConst) {
$this->constants[] = $stmt;
} else {
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
}
return $this;
}
/**
* Adds an attribute group.
*
* @param Node\Attribute|Node\AttributeGroup $attribute
*
* @return $this The builder instance (for fluid interface)
*/
public function addAttribute($attribute) {
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
return $this;
}
/**
* Returns the built class node.
*
* @return Stmt\Class_ The built class node
*/
public function getNode(): PhpParser\Node {
return new Stmt\Class_($this->name, [
'flags' => $this->flags,
'extends' => $this->extends,
'implements' => $this->implements,
'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
'attrGroups' => $this->attributeGroups,
], $this->attributes);
}
}

View File

@ -0,0 +1,50 @@
<?php declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\BuilderHelpers;
abstract class Declaration implements PhpParser\Builder {
/** @var array<string, mixed> */
protected array $attributes = [];
/**
* Adds a statement.
*
* @param PhpParser\Node\Stmt|PhpParser\Builder $stmt The statement to add
*
* @return $this The builder instance (for fluid interface)
*/
abstract public function addStmt($stmt);
/**
* Adds multiple statements.
*
* @param (PhpParser\Node\Stmt|PhpParser\Builder)[] $stmts The statements to add
*
* @return $this The builder instance (for fluid interface)
*/
public function addStmts(array $stmts) {
foreach ($stmts as $stmt) {
$this->addStmt($stmt);
}
return $this;
}
/**
* Sets doc comment for the declaration.
*
* @param PhpParser\Comment\Doc|string $docComment Doc comment to set
*
* @return $this The builder instance (for fluid interface)
*/
public function setDocComment($docComment) {
$this->attributes['comments'] = [
BuilderHelpers::normalizeDocComment($docComment)
];
return $this;
}
}

View File

@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
class EnumCase implements PhpParser\Builder {
/** @var Identifier|string */
protected $name;
/** @var ?Node\Expr */
protected ?Node\Expr $value = null;
/** @var array<string, mixed> */
protected array $attributes = [];
/** @var list<Node\AttributeGroup> */
protected array $attributeGroups = [];
/**
* Creates an enum case builder.
*
* @param string|Identifier $name Name
*/
public function __construct($name) {
$this->name = $name;
}
/**
* Sets the value.
*
* @param Node\Expr|string|int $value
*
* @return $this
*/
public function setValue($value) {
$this->value = BuilderHelpers::normalizeValue($value);
return $this;
}
/**
* Sets doc comment for the constant.
*
* @param PhpParser\Comment\Doc|string $docComment Doc comment to set
*
* @return $this The builder instance (for fluid interface)
*/
public function setDocComment($docComment) {
$this->attributes = [
'comments' => [BuilderHelpers::normalizeDocComment($docComment)]
];
return $this;
}
/**
* Adds an attribute group.
*
* @param Node\Attribute|Node\AttributeGroup $attribute
*
* @return $this The builder instance (for fluid interface)
*/
public function addAttribute($attribute) {
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
return $this;
}
/**
* Returns the built enum case node.
*
* @return Stmt\EnumCase The built constant node
*/
public function getNode(): PhpParser\Node {
return new Stmt\EnumCase(
$this->name,
$this->value,
$this->attributeGroups,
$this->attributes
);
}
}

View File

@ -0,0 +1,116 @@
<?php declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
class Enum_ extends Declaration {
protected string $name;
protected ?Identifier $scalarType = null;
/** @var list<Name> */
protected array $implements = [];
/** @var list<Stmt\TraitUse> */
protected array $uses = [];
/** @var list<Stmt\EnumCase> */
protected array $enumCases = [];
/** @var list<Stmt\ClassConst> */
protected array $constants = [];
/** @var list<Stmt\ClassMethod> */
protected array $methods = [];
/** @var list<Node\AttributeGroup> */
protected array $attributeGroups = [];
/**
* Creates an enum builder.
*
* @param string $name Name of the enum
*/
public function __construct(string $name) {
$this->name = $name;
}
/**
* Sets the scalar type.
*
* @param string|Identifier $scalarType
*
* @return $this
*/
public function setScalarType($scalarType) {
$this->scalarType = BuilderHelpers::normalizeType($scalarType);
return $this;
}
/**
* Implements one or more interfaces.
*
* @param Name|string ...$interfaces Names of interfaces to implement
*
* @return $this The builder instance (for fluid interface)
*/
public function implement(...$interfaces) {
foreach ($interfaces as $interface) {
$this->implements[] = BuilderHelpers::normalizeName($interface);
}
return $this;
}
/**
* Adds a statement.
*
* @param Stmt|PhpParser\Builder $stmt The statement to add
*
* @return $this The builder instance (for fluid interface)
*/
public function addStmt($stmt) {
$stmt = BuilderHelpers::normalizeNode($stmt);
if ($stmt instanceof Stmt\EnumCase) {
$this->enumCases[] = $stmt;
} elseif ($stmt instanceof Stmt\ClassMethod) {
$this->methods[] = $stmt;
} elseif ($stmt instanceof Stmt\TraitUse) {
$this->uses[] = $stmt;
} elseif ($stmt instanceof Stmt\ClassConst) {
$this->constants[] = $stmt;
} else {
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
}
return $this;
}
/**
* Adds an attribute group.
*
* @param Node\Attribute|Node\AttributeGroup $attribute
*
* @return $this The builder instance (for fluid interface)
*/
public function addAttribute($attribute) {
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
return $this;
}
/**
* Returns the built class node.
*
* @return Stmt\Enum_ The built enum node
*/
public function getNode(): PhpParser\Node {
return new Stmt\Enum_($this->name, [
'scalarType' => $this->scalarType,
'implements' => $this->implements,
'stmts' => array_merge($this->uses, $this->enumCases, $this->constants, $this->methods),
'attrGroups' => $this->attributeGroups,
], $this->attributes);
}
}

View File

@ -0,0 +1,73 @@
<?php declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
abstract class FunctionLike extends Declaration {
protected bool $returnByRef = false;
/** @var Node\Param[] */
protected array $params = [];
/** @var Node\Identifier|Node\Name|Node\ComplexType|null */
protected ?Node $returnType = null;
/**
* Make the function return by reference.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeReturnByRef() {
$this->returnByRef = true;
return $this;
}
/**
* Adds a parameter.
*
* @param Node\Param|Param $param The parameter to add
*
* @return $this The builder instance (for fluid interface)
*/
public function addParam($param) {
$param = BuilderHelpers::normalizeNode($param);
if (!$param instanceof Node\Param) {
throw new \LogicException(sprintf('Expected parameter node, got "%s"', $param->getType()));
}
$this->params[] = $param;
return $this;
}
/**
* Adds multiple parameters.
*
* @param (Node\Param|Param)[] $params The parameters to add
*
* @return $this The builder instance (for fluid interface)
*/
public function addParams(array $params) {
foreach ($params as $param) {
$this->addParam($param);
}
return $this;
}
/**
* Sets the return type for PHP 7.
*
* @param string|Node\Name|Node\Identifier|Node\ComplexType $type
*
* @return $this The builder instance (for fluid interface)
*/
public function setReturnType($type) {
$this->returnType = BuilderHelpers::normalizeType($type);
return $this;
}
}

View File

@ -0,0 +1,67 @@
<?php declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Stmt;
class Function_ extends FunctionLike {
protected string $name;
/** @var list<Stmt> */
protected array $stmts = [];
/** @var list<Node\AttributeGroup> */
protected array $attributeGroups = [];
/**
* Creates a function builder.
*
* @param string $name Name of the function
*/
public function __construct(string $name) {
$this->name = $name;
}
/**
* Adds a statement.
*
* @param Node|PhpParser\Builder $stmt The statement to add
*
* @return $this The builder instance (for fluid interface)
*/
public function addStmt($stmt) {
$this->stmts[] = BuilderHelpers::normalizeStmt($stmt);
return $this;
}
/**
* Adds an attribute group.
*
* @param Node\Attribute|Node\AttributeGroup $attribute
*
* @return $this The builder instance (for fluid interface)
*/
public function addAttribute($attribute) {
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
return $this;
}
/**
* Returns the built function node.
*
* @return Stmt\Function_ The built function node
*/
public function getNode(): Node {
return new Stmt\Function_($this->name, [
'byRef' => $this->returnByRef,
'params' => $this->params,
'returnType' => $this->returnType,
'stmts' => $this->stmts,
'attrGroups' => $this->attributeGroups,
], $this->attributes);
}
}

View File

@ -0,0 +1,94 @@
<?php declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
class Interface_ extends Declaration {
protected string $name;
/** @var list<Name> */
protected array $extends = [];
/** @var list<Stmt\ClassConst> */
protected array $constants = [];
/** @var list<Stmt\ClassMethod> */
protected array $methods = [];
/** @var list<Node\AttributeGroup> */
protected array $attributeGroups = [];
/**
* Creates an interface builder.
*
* @param string $name Name of the interface
*/
public function __construct(string $name) {
$this->name = $name;
}
/**
* Extends one or more interfaces.
*
* @param Name|string ...$interfaces Names of interfaces to extend
*
* @return $this The builder instance (for fluid interface)
*/
public function extend(...$interfaces) {
foreach ($interfaces as $interface) {
$this->extends[] = BuilderHelpers::normalizeName($interface);
}
return $this;
}
/**
* Adds a statement.
*
* @param Stmt|PhpParser\Builder $stmt The statement to add
*
* @return $this The builder instance (for fluid interface)
*/
public function addStmt($stmt) {
$stmt = BuilderHelpers::normalizeNode($stmt);
if ($stmt instanceof Stmt\ClassConst) {
$this->constants[] = $stmt;
} elseif ($stmt instanceof Stmt\ClassMethod) {
// we erase all statements in the body of an interface method
$stmt->stmts = null;
$this->methods[] = $stmt;
} else {
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType()));
}
return $this;
}
/**
* Adds an attribute group.
*
* @param Node\Attribute|Node\AttributeGroup $attribute
*
* @return $this The builder instance (for fluid interface)
*/
public function addAttribute($attribute) {
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
return $this;
}
/**
* Returns the built interface node.
*
* @return Stmt\Interface_ The built interface node
*/
public function getNode(): PhpParser\Node {
return new Stmt\Interface_($this->name, [
'extends' => $this->extends,
'stmts' => array_merge($this->constants, $this->methods),
'attrGroups' => $this->attributeGroups,
], $this->attributes);
}
}

View File

@ -0,0 +1,147 @@
<?php declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\BuilderHelpers;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Stmt;
class Method extends FunctionLike {
protected string $name;
protected int $flags = 0;
/** @var list<Stmt>|null */
protected ?array $stmts = [];
/** @var list<Node\AttributeGroup> */
protected array $attributeGroups = [];
/**
* Creates a method builder.
*
* @param string $name Name of the method
*/
public function __construct(string $name) {
$this->name = $name;
}
/**
* Makes the method public.
*
* @return $this The builder instance (for fluid interface)
*/
public function makePublic() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC);
return $this;
}
/**
* Makes the method protected.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeProtected() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED);
return $this;
}
/**
* Makes the method private.
*
* @return $this The builder instance (for fluid interface)
*/
public function makePrivate() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE);
return $this;
}
/**
* Makes the method static.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeStatic() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::STATIC);
return $this;
}
/**
* Makes the method abstract.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeAbstract() {
if (!empty($this->stmts)) {
throw new \LogicException('Cannot make method with statements abstract');
}
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::ABSTRACT);
$this->stmts = null; // abstract methods don't have statements
return $this;
}
/**
* Makes the method final.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeFinal() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::FINAL);
return $this;
}
/**
* Adds a statement.
*
* @param Node|PhpParser\Builder $stmt The statement to add
*
* @return $this The builder instance (for fluid interface)
*/
public function addStmt($stmt) {
if (null === $this->stmts) {
throw new \LogicException('Cannot add statements to an abstract method');
}
$this->stmts[] = BuilderHelpers::normalizeStmt($stmt);
return $this;
}
/**
* Adds an attribute group.
*
* @param Node\Attribute|Node\AttributeGroup $attribute
*
* @return $this The builder instance (for fluid interface)
*/
public function addAttribute($attribute) {
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
return $this;
}
/**
* Returns the built method node.
*
* @return Stmt\ClassMethod The built method node
*/
public function getNode(): Node {
return new Stmt\ClassMethod($this->name, [
'flags' => $this->flags,
'byRef' => $this->returnByRef,
'params' => $this->params,
'returnType' => $this->returnType,
'stmts' => $this->stmts,
'attrGroups' => $this->attributeGroups,
], $this->attributes);
}
}

View File

@ -0,0 +1,45 @@
<?php declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Stmt;
class Namespace_ extends Declaration {
private ?Node\Name $name;
/** @var Stmt[] */
private array $stmts = [];
/**
* Creates a namespace builder.
*
* @param Node\Name|string|null $name Name of the namespace
*/
public function __construct($name) {
$this->name = null !== $name ? BuilderHelpers::normalizeName($name) : null;
}
/**
* Adds a statement.
*
* @param Node|PhpParser\Builder $stmt The statement to add
*
* @return $this The builder instance (for fluid interface)
*/
public function addStmt($stmt) {
$this->stmts[] = BuilderHelpers::normalizeStmt($stmt);
return $this;
}
/**
* Returns the built node.
*
* @return Stmt\Namespace_ The built node
*/
public function getNode(): Node {
return new Stmt\Namespace_($this->name, $this->stmts, $this->attributes);
}
}

View File

@ -0,0 +1,149 @@
<?php declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\BuilderHelpers;
use PhpParser\Modifiers;
use PhpParser\Node;
class Param implements PhpParser\Builder {
protected string $name;
protected ?Node\Expr $default = null;
/** @var Node\Identifier|Node\Name|Node\ComplexType|null */
protected ?Node $type = null;
protected bool $byRef = false;
protected int $flags = 0;
protected bool $variadic = false;
/** @var list<Node\AttributeGroup> */
protected array $attributeGroups = [];
/**
* Creates a parameter builder.
*
* @param string $name Name of the parameter
*/
public function __construct(string $name) {
$this->name = $name;
}
/**
* Sets default value for the parameter.
*
* @param mixed $value Default value to use
*
* @return $this The builder instance (for fluid interface)
*/
public function setDefault($value) {
$this->default = BuilderHelpers::normalizeValue($value);
return $this;
}
/**
* Sets type for the parameter.
*
* @param string|Node\Name|Node\Identifier|Node\ComplexType $type Parameter type
*
* @return $this The builder instance (for fluid interface)
*/
public function setType($type) {
$this->type = BuilderHelpers::normalizeType($type);
if ($this->type == 'void') {
throw new \LogicException('Parameter type cannot be void');
}
return $this;
}
/**
* Make the parameter accept the value by reference.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeByRef() {
$this->byRef = true;
return $this;
}
/**
* Make the parameter variadic
*
* @return $this The builder instance (for fluid interface)
*/
public function makeVariadic() {
$this->variadic = true;
return $this;
}
/**
* Makes the (promoted) parameter public.
*
* @return $this The builder instance (for fluid interface)
*/
public function makePublic() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC);
return $this;
}
/**
* Makes the (promoted) parameter protected.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeProtected() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED);
return $this;
}
/**
* Makes the (promoted) parameter private.
*
* @return $this The builder instance (for fluid interface)
*/
public function makePrivate() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE);
return $this;
}
/**
* Makes the (promoted) parameter readonly.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeReadonly() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::READONLY);
return $this;
}
/**
* Adds an attribute group.
*
* @param Node\Attribute|Node\AttributeGroup $attribute
*
* @return $this The builder instance (for fluid interface)
*/
public function addAttribute($attribute) {
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
return $this;
}
/**
* Returns the built parameter node.
*
* @return Node\Param The built parameter node
*/
public function getNode(): Node {
return new Node\Param(
new Node\Expr\Variable($this->name),
$this->default, $this->type, $this->byRef, $this->variadic, [], $this->flags, $this->attributeGroups
);
}
}

View File

@ -0,0 +1,161 @@
<?php declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\BuilderHelpers;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\ComplexType;
class Property implements PhpParser\Builder {
protected string $name;
protected int $flags = 0;
protected ?Node\Expr $default = null;
/** @var array<string, mixed> */
protected array $attributes = [];
/** @var null|Identifier|Name|ComplexType */
protected ?Node $type = null;
/** @var list<Node\AttributeGroup> */
protected array $attributeGroups = [];
/**
* Creates a property builder.
*
* @param string $name Name of the property
*/
public function __construct(string $name) {
$this->name = $name;
}
/**
* Makes the property public.
*
* @return $this The builder instance (for fluid interface)
*/
public function makePublic() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PUBLIC);
return $this;
}
/**
* Makes the property protected.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeProtected() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PROTECTED);
return $this;
}
/**
* Makes the property private.
*
* @return $this The builder instance (for fluid interface)
*/
public function makePrivate() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::PRIVATE);
return $this;
}
/**
* Makes the property static.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeStatic() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::STATIC);
return $this;
}
/**
* Makes the property readonly.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeReadonly() {
$this->flags = BuilderHelpers::addModifier($this->flags, Modifiers::READONLY);
return $this;
}
/**
* Sets default value for the property.
*
* @param mixed $value Default value to use
*
* @return $this The builder instance (for fluid interface)
*/
public function setDefault($value) {
$this->default = BuilderHelpers::normalizeValue($value);
return $this;
}
/**
* Sets doc comment for the property.
*
* @param PhpParser\Comment\Doc|string $docComment Doc comment to set
*
* @return $this The builder instance (for fluid interface)
*/
public function setDocComment($docComment) {
$this->attributes = [
'comments' => [BuilderHelpers::normalizeDocComment($docComment)]
];
return $this;
}
/**
* Sets the property type for PHP 7.4+.
*
* @param string|Name|Identifier|ComplexType $type
*
* @return $this
*/
public function setType($type) {
$this->type = BuilderHelpers::normalizeType($type);
return $this;
}
/**
* Adds an attribute group.
*
* @param Node\Attribute|Node\AttributeGroup $attribute
*
* @return $this The builder instance (for fluid interface)
*/
public function addAttribute($attribute) {
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
return $this;
}
/**
* Returns the built class node.
*
* @return Stmt\Property The built property node
*/
public function getNode(): PhpParser\Node {
return new Stmt\Property(
$this->flags !== 0 ? $this->flags : Modifiers::PUBLIC,
[
new Node\PropertyItem($this->name, $this->default)
],
$this->attributes,
$this->type,
$this->attributeGroups
);
}
}

View File

@ -0,0 +1,65 @@
<?php declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser\Builder;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Stmt;
class TraitUse implements Builder {
/** @var Node\Name[] */
protected array $traits = [];
/** @var Stmt\TraitUseAdaptation[] */
protected array $adaptations = [];
/**
* Creates a trait use builder.
*
* @param Node\Name|string ...$traits Names of used traits
*/
public function __construct(...$traits) {
foreach ($traits as $trait) {
$this->and($trait);
}
}
/**
* Adds used trait.
*
* @param Node\Name|string $trait Trait name
*
* @return $this The builder instance (for fluid interface)
*/
public function and($trait) {
$this->traits[] = BuilderHelpers::normalizeName($trait);
return $this;
}
/**
* Adds trait adaptation.
*
* @param Stmt\TraitUseAdaptation|Builder\TraitUseAdaptation $adaptation Trait adaptation
*
* @return $this The builder instance (for fluid interface)
*/
public function with($adaptation) {
$adaptation = BuilderHelpers::normalizeNode($adaptation);
if (!$adaptation instanceof Stmt\TraitUseAdaptation) {
throw new \LogicException('Adaptation must have type TraitUseAdaptation');
}
$this->adaptations[] = $adaptation;
return $this;
}
/**
* Returns the built node.
*
* @return Node The built node
*/
public function getNode(): Node {
return new Stmt\TraitUse($this->traits, $this->adaptations);
}
}

Some files were not shown because too many files have changed in this diff Show More