Skip to content

Instantly share code, notes, and snippets.

@ChriRas
Last active June 21, 2019 12:55
Show Gist options
  • Save ChriRas/312458232d6066573fd8964f0ccc0856 to your computer and use it in GitHub Desktop.
Save ChriRas/312458232d6066573fd8964f0ccc0856 to your computer and use it in GitHub Desktop.
Regex to tranform Code in PHPstom using search&replace

Change a 'define' into a 'const'

From define('WT_TYPE', 2); to public const WT_TYPE = 2;

search: define\('([\w]*)', ([\w]*)\); replace: public const $1 = $2;

Change a get an array field into a function call

From this 'return $this->attributes['example'];' to 'return $this->getAttribute('example');'

search: return \$this->attributes\['([\w]*)'\]; replace: return \$this->getAttribute\('$1'\);

From this '$this->attributes['example'] = $example;' to '$this->setAttribute('example', $example);'

search: \$this->attributes\['([\w]*)'\] = (\$[\w]*); replace: \$this->setAttribute\('$1'\,$2);

Change a string value into a constant

Part 1

From 'test', to

   * Attribute: test
   */
  private const ATTRIBUTE_TEST = 'test';

search: '([\w]*)', replace:

   /**
     * Attribute: $1
     */
    private const ATTRIBUTE_\U$1 = '\L$1';

Part 2

From 'test' to

self::ATTRIBUTE_TEST

search: '([\w]*)', replace: self::ATTRIBUTE_\U$1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment