duanpa5237 2018-01-29 17:55
浏览 106
已采纳

替换类中的所有出现的$符号和sublime文本中的第一个函数

I did a really stupid mistake after reading about var in PHP7 and I mistakenly thought that I don't need neither var nor public to declare variables inside of a class (outside of it's methods). So I removed it from 400+ files and did some refactoring on pretty much all of them. So I can't just revert back, because that would leave me with even more work.

Now I get a fatal error and I need to add either private or public to all variables declared that way. I'm not sure if any of these classes actually need public variables, probably private should be enough.

But I can't figure out if it's even possible to do in Sublime Text 3. I tried many different combinations of regex, but none of them worked. I'm guessing that I might need a script to run though all files instead, but maybe I'm overlooking something...

So I have something like this:

class MyClass {
    $myvar1;
    $myvar2;

    function __construct($myvar1 = NULL, $myvar2 = array()) {
        $this->myvar1 = $myvar1;    
        $this->myvar2 = $myvar2;    
    }
}

And I want to turn it into this:

class MyClass {
    private $myvar1;
    private $myvar2;

    function __construct($myvar1 = NULL, $myvar2 = array()) {
        $this->myvar1 = $myvar1;    
        $this->myvar2 = $myvar2;    
    }
}

EDIT: Please note that there are no comments (neither C/C++ nor # style) between the class declaration and its first function. Also string notation should not be considered. The solution should solve specifically this issue, because it seems to be impossible to make it universal for all possible cases. It seems that using \G modifier is the key to solve this particular issue the way I wanted to. So use the answer with caution.

I think I need to replace all occurrences of $ with private $ in between class and the first occurrence of function. Could it be possible just with Sublime Text 3?

So far I fail at even finding all occurences of a pattern that starts with class and ends with the first function. Currently I'm trying to improve the following regex: (?s)class (.*?){.+function. For some reason the selection ends at the last function, not the first one and I can't figure out how to fix that. As the worst option I could use this pattern to search through all 400+ files and then edit them manually, but that would be a pain, of course...

PS: is it even a good practice to declare them there if I assign them in the __construct method anyway?

  • 写回答

2条回答 默认 最新

  • douchao5864 2018-01-29 18:17
    关注

    The following regex will only match variables after the class's opening and will not work after a comment or constant declaration. See regex in use here

    (?:\bclass \w+\s*{\s*|\G(?!\A)\s*)\K(\$\w+\s*(?:;|=.*;))
    

    For a regex that works around comments and constants as well you can use the following (see regex in use here). Note that this, once again, only works for variables at the top of a class:

    (?:\bclass \w+\s*{\s*|\G(?!\A)\s*(?:(?://|#).*$|/\*[\s\S]*?\*/|\bconst[^;]*;)\s*)\K(\$\w+\s*(?:;|=.*;))
    

    If variables are declared after the top of the class, your best bet for a quick and dirty solution is to use the whitespace as an indicator of what belongs to the class and what doesn't. Assuming you use proper indentation of either 1 tab character or 4 space characters, you can use ^(?:\t| {4})\K(\$\w+)

    Replacement: private $1

    • (?:\bclass \w+\s*{\s*|\G(?!\A)\s*) Matches either of the following:
      • \bclass \w+\s*{\s*
        • \b Assert position as a word boundary
        • class Match this literally
        • \w+ Match one or more word characters
        • \s*{\s* Match any number of whitespace, followed by {, followed by any number of whitespace
      • \G(?!\A)\s* Assert position at the end of the previous match and match any number of whitespace
    • \K Resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
    • (\$\w+\s*(?:;|=.*;)) Capture the following into capture group 1
      • \$\w+\s* Match $ literally, followed by one or more word characters then any number of whitespace
      • (?:;|=.*;) Match either ; or = followed by any character any number of times and then ;
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)