Kayerraga,
I did a Porto-Lisbon-Madrid trip with our 21 year old son in Dec 2017 and offer the following thoughts:
-Loved all three cities. My personal opinion would be that Lisbon deserves the most time and Porto the least time. This of course depends on your interests (for example if you are a wine enthusiast then Porto might deserve the most time);
-It is challenging to get from Portugal to Spain. We took an overnight train from Lisbon to Madrid and it was rough travel (hard to sleep). I think there is also an overnight train to Madrid from Porto. Overnight trains are efficient, but this might be the rare case where flying is the better option;
-While in Lisbon, I recommend at least one day trip to Sintra, which is easily accessible by train. Sintra is very popular (i.e. crowded) so be strategic about your visit there (essentially get there early and have an itinerary in mind before you arrive);
-While in Madrid, I recommend at least one day trip to Toledo or Segovia, which are both easily accessible by train. Personal preference as to which is best, but I preferred Segovia because of its castle;
-Public transportation in Lisbon and Madrid are great. We walked everywhere in Porto because it is small, so I don’t know if Porto even has a subway or tram system;
-We stayed near the center of town in all three cities, which I recommend for efficiency sake;
-Things to Do that I recommend in each city: Porto - port lodge(s), walk across the bridge, boat tour and Harry Potter library (can’t remember the name). Lisbon - Alfa’s tour, Heronimous monetary, Belem tower and tram 28. Madrid - art museums, royal palace, plaza major, Ritero Park).
Good luck and safe travels.