Skip to content

Commit 6500f76

Browse files
jorritmghoneimy
authored andcommitted
Normalize line endings to generate consistent line endings on Windows
1 parent f72823c commit 6500f76

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/SchemaGenerator/CodeGenerator/CodeFile/ClassFile.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected function generateFileContents(): string
125125
if (!empty($properties)) $properties = PHP_EOL . $properties;
126126
$methods = $this->generateMethods();
127127

128-
return sprintf(
128+
$contents = sprintf(
129129
static::FILE_FORMAT,
130130
$namespace,
131131
$imports,
@@ -135,6 +135,7 @@ protected function generateFileContents(): string
135135
$properties,
136136
$methods
137137
);
138+
return $this->normalizeLineEndings($contents);
138139
}
139140

140141
/**

src/SchemaGenerator/CodeGenerator/CodeFile/TraitFile.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ public function addProperty(string $name, $value = null)
104104
public function addMethod(string $methodString, bool $isDeprecated = false, ?string $deprecationReason = null)
105105
{
106106
if (!empty($methodString)) {
107-
$this->methods[] = $this->prependDeprecationComment($methodString, $isDeprecated, $deprecationReason);
107+
$methodString = $this->prependDeprecationComment($methodString, $isDeprecated, $deprecationReason);
108+
109+
// Normalize line endings here to make replacements in generateMethods() work.
110+
$methodString = $this->normalizeLineEndings($methodString);
111+
$this->methods[] = $methodString;
108112
}
109113
}
110114

@@ -126,7 +130,8 @@ protected function generateFileContents(): string
126130
if (!empty($properties)) $properties = PHP_EOL . $properties;
127131
$methods = $this->generateMethods();
128132

129-
return sprintf(static::FILE_FORMAT, $namespace, $imports, $className, $properties, $methods);
133+
$contents = sprintf(static::FILE_FORMAT, $namespace, $imports, $className, $properties, $methods);
134+
return $this->normalizeLineEndings($contents);
130135
}
131136

132137
/**
@@ -224,4 +229,14 @@ protected function prependDeprecationComment(string $code, bool $isDeprecated, ?
224229

225230
return $code;
226231
}
232+
233+
protected function normalizeLineEndings(string $code)
234+
{
235+
$code = str_replace("\r", '', $code);
236+
if (PHP_EOL !== "\n") {
237+
$code = str_replace("\n", PHP_EOL, $code);
238+
}
239+
240+
return $code;
241+
}
227242
}

0 commit comments

Comments
 (0)