Skip to content

Commit b78cab1

Browse files
authored
Merge pull request #53 from shreya0204/shreya0204-50-deleting-constants-deletes-more-lines
Deletion of constants deletes more lines than expected
2 parents 596d6bd + 2d3dab5 commit b78cab1

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/WPConfigTransformer.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,19 @@ public function remove( $type, $name ) {
226226
return false;
227227
}
228228

229-
$pattern = sprintf( '/(?<=^|;|<\?php\s|<\?\s)%s\s*(\S|$)/m', preg_quote( $this->wp_configs[ $type ][ $name ]['src'], '/' ) );
230-
$contents = preg_replace( $pattern, '$1', $this->wp_config_src );
229+
if ( 'constant' === $type ) {
230+
$pattern = sprintf(
231+
"/\bdefine\s*\(\s*['\"]%s['\"]\s*,\s*(('[^']*'|\"[^\"]*\")|\s*(?:[\s\S]*?))\s*\)\s*;\s*/mi",
232+
preg_quote( $name, '/' )
233+
);
234+
} else {
235+
$pattern = sprintf(
236+
'/^\s*\$%s\s*=\s*[\s\S]*?;\s*$/mi',
237+
preg_quote( $name, '/' )
238+
);
239+
}
240+
241+
$contents = preg_replace( $pattern, '', $this->wp_config_src );
231242

232243
return $this->save( $contents );
233244
}

tests/AddRemoveTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,25 @@ public function testRemoveStringVariables() {
142142
}
143143
}
144144

145+
public function testRemoveConstantWithConcatenation() {
146+
// Set up the initial state by defining a constant with concatenation
147+
$name = 'TEST_USER_PATH';
148+
$value = "'/var/www/' . get_current_user()";
149+
$this->assertTrue( self::$config_transformer->add( 'constant', $name, $value, array( 'raw' => true ) ), "Adding constant {$name}" );
150+
151+
// Define another constant to check if it remains untouched
152+
$second_name = 'TEST_THIS_AND';
153+
$second_value = "md5('that')";
154+
$this->assertTrue( self::$config_transformer->add( 'constant', $second_name, $second_value, array( 'raw' => true ) ), "Adding constant {$second_name}" );
155+
156+
// Remove the first constant and check the result
157+
$this->assertTrue( self::$config_transformer->remove( 'constant', $name ), "Removing constant {$name}" );
158+
$this->assertFalse( self::$config_transformer->exists( 'constant', $name ), "Check {$name} does not exist" );
159+
160+
// Ensure the second constant is still present after the removal
161+
$this->assertTrue( self::$config_transformer->exists( 'constant', $second_name ), "Check {$second_name} still exists" );
162+
}
163+
145164
public function testAddConstantNoPlacementAnchor() {
146165
$this->expectException( Exception::class );
147166
$this->expectExceptionMessage( 'Unable to locate placement anchor.' );

0 commit comments

Comments
 (0)