SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Automated Refactoring

    I'm curious to know if anyone knows of an active project or has ever been involved in a project to build an automated refactoring tool for PHP. I've been studying a lot of papers from the writers of the SmallTalk Refactoring Browser and from various other experts in the field who have developed similar tools for Java and C#, including the one in VS2005... but I haven't seen a drip of information about doing this for PHP.

    Just to be clear, I don't necessarily think the tool should be written in PHP, just to work on PHP. If anyone can point me to some useful information, it would be much appreciated.

    Thanks in advance.

  2. #2
    SitePoint Addict been's Avatar
    Join Date
    May 2002
    Location
    Gent, Belgium
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To begin; I don't know of any

    Not an answer you were looking for, I'm sure, but I thought I'd post at least something so you wouldn't think nobody cares, this is actually a nice community

    I threw up a little ball in this post on how the use of type hinting and interfaces would probably be benificial to a refactoring tool, but that ball just seemed to have dropped dead on the floor, which might mean either of a bunch of things, amongst others:

    1. Noboby actually cares about refactoring tool?
    2. I'm "quantité négligable"?
    3. I was dead wrong, but nobody took the effort to tell me (see 2.)?
    4. It might have sounded scary, since it would make the syntax resemble more that of a statically typed language?
    5. ...
    Per
    Everything
    works on a PowerPoint slide

  3. #3
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by been
    ... this is actually a nice community ...
    ...
    1. Noboby actually cares about refactoring tool?
    2. I'm "quantité négligable"?
    3. I was dead wrong, but nobody took the effort to tell me (see 2.)?
    4. It might have sounded scary, since it would make the syntax resemble more that of a statically typed language?
    5. ...
    While I agree with you that this is (mostly) a nice community, you are not the only one left with the feeling you'll never belong to the 3l1t3 ...

    Unfortunately, sometimes it looks like some people around have troubles coming down to earth and keeping their ego's from feeding themselves ...

    PS.- I'm sorry if I'm giving the impression to be hijacking this thread, but I couldn't avoid to answer.
    There’s more than one way to skin a cat.

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunately, sometimes it looks like some people around have troubles coming down to earth and keeping their ego's from feeding themselves ...


    There may actually be some truth in that though... If your interested in refactoring there are several things you could do? Try looking at www.martinfowler.com for some refactoring advice and tips.

    Interfaces and class abstractions could in some (small) manner help to refactor your classes but as I see it, this is more towards the design area rather than the development area.

    If you can strive for a naming convention behind your Interfaces, you could find it a lot easier to structure your class hierarchies? I am more and more tending to define an Interface as a set responsibility of what it's implementation has to do but this is just an idea I have, and I'm not sure at the moment if it's a good or a bad thing?

  5. #5
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by been
    1. Noboby actually cares about refactoring tool?
    I do! Unfortunately creating one is a major task. They never managed to build one for C++ after all.

    By the way, this has nothing to do with type hinting. The Smalltalk refactoring browser came first. Type hinting is a stop gap, where you tell the IDE what the types are. A tool with the power to refactor would know what it is was doing anyway. In fact type hinting would probably be misleading, as when you are mid-refactor anything could be going on.

    Quote Originally Posted by been
    2. I'm "quantité négligable"?
    Well, you could get around this by starting such a project . Not in an official way, but as a learning exercise over the next 2-3 years. You would need to know Java, then you would need to know how Eclipse plug-ins work, then how Eclipse does it's refactoring and finally port it to the PHP version. In between you would have to read all of the refactoring papers ever written (a few dozen tops) and learn the necessary algorithms.

    Then spend 2 years actually writing the production version.

    I'm serious by the way. You would ascend to god hood in the process (well above the average Zend/PHPer) and you could do all of this part time. You've just got to have a plan.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  6. #6
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by been
    2. I'm "quantité négligable"?
    Quote Originally Posted by lastcraft
    Well, you could get around this by starting such a project . Not in an official way, but as a learning exercise over the next 2-3 years. You would need to know Java, then you would need to know how Eclipse plug-ins work, then how Eclipse does it's refactoring and finally port it to the PHP version. In between you would have to read all of the refactoring papers ever written (a few dozen tops) and learn the necessary algorithms.

    Then spend 2 years actually writing the production version.
    And all that to end up totally eclipsed yourself.
    There’s more than one way to skin a cat.

  7. #7
    SitePoint Addict been's Avatar
    Join Date
    May 2002
    Location
    Gent, Belgium
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lastcraft
    I do!
    Don't worry, I know you do

    Btw, for the record:
    It wasn't my intention to come across pitiable or to take a wack at the so called "elite" of this forum, I was just expressing an observation while in the meantime offering some possible reasons...

    Anyway, back to the topic...

    Quote Originally Posted by lastcraft
    By the way, this has nothing to do with type hinting. The Smalltalk refactoring browser came first. Type hinting is a stop gap, where you tell the IDE what the types are. A tool with the power to refactor would know what it is was doing anyway. In fact type hinting would probably be misleading, as when you are mid-refactor anything could be going on.
    That is of course what makes the task of building a refactoring tool so daunting (and probably time consuming), one would have to build a tool to figure out stuff like
    PHP Code:
    $className $condition "ClassOne" "ClassTwo";
      
    $obj =& new $className();
     
    $obj->someMethod(); 
    enough to know that there'll be trouble when someMethod() will be renamed in classOne when there's no actual dependency between ClassOne and ClassTwo.

    Quote Originally Posted by lastcraft
    Well, you could get around this by starting such a project...
    I'm thinking about it actually, but I have doubts that I'd start on it any time soon; I've got one more year to go in evening school, which will be all Java, I will need to focus on that for quite a bit.
    I could maybe do it as a school project, but I'm affraid that I wouldn't have enough time to come with something decent enough, 1 year is just too little, as Marcus already pointed out.
    I also need to prioritize; I'm also considering building a contract tool for PHP...

    Quote Originally Posted by lastcraft
    I'm serious by the way.
    Once again, don't worry, I know you are
    Per
    Everything
    works on a PowerPoint slide

  8. #8
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by been
    It wasn't my intention [...] to take a wack at the so called "elite" of this forum, ...
    To end it up, neither was mine as the word "elite" is meaningless to me. It was a figure of speech.
    Last edited by nacho; Aug 2, 2005 at 06:50. Reason: typos
    There’s more than one way to skin a cat.

  9. #9
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well at least people finally replied...

    One of the worst problems with coming up with a tool for refactoring is making it behave in some reasonable way when people write retarded code, like creating classes or calling methods using data directly from forms and such.

    Beyond that though, it seems to me like it would amount to a pretty beastly project. My best estimate at a starting point would be to develop a sort of tokenizer that worked heirarchically with sufficient ability to trace back through variable and reference chains. Somehow break up the information gathered into blocks, then create some sort of hash or code or something, and use prolog to help pin down duplication.

    I think that would be biting off more than I can chew, at least with my lack of spare time.

    But by all means, if someone else wants to do it, I'll gladly.. uh.. use it? =)

  10. #10
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    If you cannot solve the complete problem, then the trick is to solve part of the problem and let others carry things forward. Your first step was the PHP tokeniser. You could just do that.

    Alternatively take a vertical approach. Write a tool to rename methods within a code base. It could start with simple search and replace and then build in complexity as trickier and trickier cases were added.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  11. #11
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even if this tool was very simple and just let you get a high-level view of your code it would be helpful in thinking about refactoring. A first phase might be a read only tool that made some simple attempts to tell you where you might want to refactor.
    Christopher

  12. #12
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's an idea that may be moderately harebrained but seems to work up to a point: process the program the unsophisticated working-class way, using regular expressions.
    (Edit: Re-reading Marcus' post, I see that this is related to his "simple search and replace".) I've been doing this in Vim; it's imperfect, and I wouldn't want to have anyone else depending on my code, but it's useful.

    I started out in Vim script which is not the most advanced programming language in the world. Since I'm test infected, I couldn't bear programming in a language that has no test framework (and I wasn't ready to create VimUnit), so I switched to Python, which can be compiled into Vim and used to program the editor. Using Python to modify PHP code is weird, but possible.

    Here's an example. It's unsophisticated, probably buggy, and it's one of about 6 Python classes I've ever written, so don't expect brilliant Python programming. It's only intended to demonstrate the principle. But it works.
    Code:
    class RenameInstanceVariable:
        def __init__(self,vim):
            self.vim = vim
            self.oldName = ''
            self.newName = ''
    
        def execute(self):
            self.oldName = self.vim.input('Variable name',self.getSuggestedVariable())
            self.newName = self.vim.input('New variable name','')
            range = self.vim.getClassRange()
            lines = self.process(range.getLines())
            range.setLines(lines)
    
        def getSuggestedVariable(self):
            return self.vim.currentWord().replace('$','')
    
        def process(self,lines):
            for i in range(0,len(lines)):
                lines[i] = re.sub('(private|protected|public)\s+\$'+self.oldName+';',
                    "\g<1> $"+self.newName+';',
                    lines[i])
                lines[i] = re.sub('\$this->'+self.oldName+"(\W)",
                        "$this->"+self.newName+"\g<1>",
                        lines[i])
            return lines
    When run inside Vim, the vim object exercises the actual editor. In the tests, I replace it with a mock object. So this particular piece of code can be tested without Vim itself.

    getClassRange() returns an object representing the cursor position range of the class containing the current cursor position.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  13. #13
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Montreal
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of writing the php parser all over again, why not look at the overall structure of the application? For example, you could begin by using an adapted phpDoc-like syntax to determine the relationships in the application. That would give you an idea of the different has-a, is-a, returns-a, accepts-a relationships. From there, that might give you a better chance of getting to the actual refactoring algorithms than to have to to first parse every nuance of the php language.

    I hope you take a stab at this. It would be immensely interesting to see what you come up with.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •